# -*- sh -*- # hello world # print a simple string on stdout echo Hello World # argv # access command line parameters (no segmentation fault accepted, nor silent exception, so some languages must explicitly check the presence of the argument) echo $1 # env # access environment variable echo $HOME # test file exists # return exit code error (non zero) if a file does not exist [ -e /etc/mtab ] # test file readable # return exit code error (non zero) if a file is not readable [ -r /etc/mtab ] # formatting # print integers in a simple formatted string a=1; b=2; echo "$a + $b = $[$a + $b]" # system # call an external program and check the return value false || echo "false failed" 1>&2; echo done # sed in place # remove #-comments from a file (modifying the file, i.e. in place) sed -i -e "s/#.*//" $1 # compile what must be # find and compile .c files into .o when the .o is old or absent for c in `find -name "*.c"`; do o=`echo $c | sed 's/.c$/.o/'` if [ "$c" -nt "$o" ]; then echo "compiling $c to $o" gcc -c -o "$o" "$c" fi done # grep # grep with -F -i -h handling, usage, grep'ing many files opts='' while [ -n "$1" ]; do case $1 in -i) opts="-i $opts" ;; -F) opts="-F $opts" ;; -h) h=1 ;; *) break ;; esac shift done if [ $# = 0 ] || [ -n "$h" ]; then echo "usage: grep [-F] [-i] regexp [files...]" exit 1 fi r=$1 shift grep $opts $r "$@"