|
|||
Department of Engineering | |
University of Cambridge > Engineering Department > computing help |
#
the rest of the line isn't executed. These comment lines are worth reading.
# Example 0 : While loop. Keeping looping while i is less than 10 # The first line creates a variable. Note that to read a # variable you need to put a '$' before its name i=0 while [ $i -lt 10 ] do echo i is $i let i=$i+1 done
# Example 1 : While loop. # This script keeps printing the date. 'true' is a command # that does nothing except return a true value. # Use ^C (Ctrl-C) to stop it. while true do echo "date is" date done
# example 2: For Loop. # Do a letter, word and line count of all the files in # the current directory. # The `*' below is expanded to a list of files. The # variable `file' successively takes the value of # these filenames. Preceding a variable name by `$' # gives its value. for file in * do echo "wc $file gives" wc $file done
# Example 3: If. # like the above, but doesn't try to run wc on directories for file in * do if [ ! -d $file ] #ie: if $file isn't a directory then echo "wc $file gives" wc $file else echo "$file is a directory" fi done
# Example 4 : Case - a multiple if # Move to the user's home directory cd for file in .?* do #Now check for some common filenames. case $file in .kshrc) echo "You have a Korn Shell set-up file";; .bashrc) echo "You have a Bash Shell set-up file";; .Xdefaults) echo "You have an X resource file";; .profile) echo "You have a shell login file";; esac done