Skip to content

Instantly share code, notes, and snippets.

@forestbaker
Last active December 5, 2015 02:11
Show Gist options
  • Save forestbaker/3acd4fec03136f231709 to your computer and use it in GitHub Desktop.
Save forestbaker/3acd4fec03136f231709 to your computer and use it in GitHub Desktop.
performance shell scripting
# The first method opens file.txt, writes one line, then closes file.txt. Then repeats those three steps 100 times
# The second method opens file.txt, writes 100 lines and closes file.txt
#!/usr/bin/sh
# typical use of a while loop -
# while $count is less than or equal to 100.
# append the value of $count to file.txt
count=1
while [ $count -le 100 ]
do
echo $count >> file.txt
let count=count+1
done
#!/usr/bin/sh
# better
# instead of opening a file and writing the value of $count at each loop iteration ...
# write the results of each iteration once when the loop completes
count=1
while [ $count -le 100 ]
do
echo $count
let count=count+1
done > file.txt
# best
n=0
substnum=
while read grp
do
echo "^git-..*[ ]$grp"
substnum="$substnum${substnum:+;}s/[ ]$grp/$n/"
n=$(($n+1))
done <"$grps" >"$match"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment