Introducing UNIX and Linux |
Introduction to shellsOverview |
The 'if' statementFor making simple decisions based on a command's exit status,
This could be rewritten using
Note that the line that starts with This mechanism is very similar to that used in Pascal or C. You
may find it easier, or clearer, to use than Worked exampleWrite a script to inform you whether or not you have used more
than 100k of disk space. # Evaluate the number of kilobytes of storage used KBYTES=$( du -s ~ | cut -f2 -d' ') # Check the value of KBYTES .. if [ $KBYTES -gt 100 ] # and display message if >100 then echo "You have used more than 100k" # and display message if <=100 else echo "You have used less than 100k" fiUsing || and && , this could have
been coded so:
KBYTES=$( du -s ~ | cut -f2 -d' ') ( [ $KBYTES -gt 100 ] && echo "You have used more than 100k" ) || [ $KBYTES -gt 100 ] || echo "You have used less than 100k" |
Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck