Anything between then and fi (if backwards) will be executed only if the test (between the square brackets) is true.
Let's look at a simple example:
- #!/bin/bash
- # Basic if statement
- if [ $1 -gt 100 ]
- then
- echo Hey that\'s a large number.
- pwd
- fi
- date
The square brackets ( [ ] ) in the if statement above are actually a reference to the command test. This means that all of the operators that test allows may be used here as well. Look up the man page for test to see all of the possible operators (there are quite a few) but some of the more common ones are listed below.
Operator | Description |
---|---|
! EXPRESSION | The EXPRESSION is false. |
-n STRING | The length of STRING is greater than zero. |
-z STRING | The lengh of STRING is zero (ie it is empty). |
STRING1 = STRING2 | STRING1 is equal to STRING2 |
STRING1 != STRING2 | STRING1 is not equal to STRING2 |
INTEGER1 -eq INTEGER2 | INTEGER1 is numerically equal to INTEGER2 |
INTEGER1 -gt INTEGER2 | INTEGER1 is numerically greater than INTEGER2 |
INTEGER1 -lt INTEGER2 | INTEGER1 is numerically less than INTEGER2 |
-d FILE | FILE exists and is a directory. |
-e FILE | FILE exists. |
-r FILE | FILE exists and the read permission is granted. |
-s FILE | FILE exists and it's size is greater than zero (ie. it is not empty). |
-w FILE | FILE exists and the write permission is granted. |
-x FILE | FILE exists and the execute permission is granted. |
A few points to note:
- = is slightly different to -eq. [ 001 = 1 ] will return false as = does a string comparison (ie. character for character the same) whereas -eq does a numerical comparison meaning [ 001 -eq 1 ] will return true.
- When we refer to FILE above we are actually meaning a path. Remember that a path may be absolute or relative and may refer to a file or a directory.
Because [ ] is just a reference to the command test we may experiment and trouble shoot with test on the command line to make sure our understanding of its behaviour is correct.
Example2
- #!/bin/bash
- # Nested if statements
- if [ $1 -gt 100 ]
- then
- echo Hey that\'s a large number.
- if (( $1 % 2 == 0 ))
- then
- echo And is also an even number.
- fi
- fi
Example3
- #!/bin/bash
- # elif statements
- if [ $1 -ge 18 ]
- then
- echo You may go to the party.
- elif [ $2 == 'yes' ]
- then
- echo You may go to the party but be back before midnight.
- else
- echo You may not go to the party.
- fi
Sometimes we only want to do something if multiple conditions are met. Other times we would like to perform the action if one of several condition is met. We can accommodate these with boolean operators.
- and - &&
- or - ||
For instance maybe we only want to perform an operation if the file is readable and has a size greater than zero.
- #!/bin/bash
- # and example
- if [ -r $1 ] && [ -s $1 ]
- then
- echo This file is useful.
- fi
Example or
- #!/bin/bash
- # or example
- if [ $USER == 'bob' ] || [ $USER == 'andy' ]
- then
- ls -alh
- else
- ls
- fi
Example:case
- #!/bin/bash
- # case example
- case $1 in
- start)
- echo starting
- ;;
- stop)
- echo stoping
- ;;
- restart)
- echo restarting
- ;;
- *)
- echo don\'t know
- ;;
- esac
Now let's look at a slightly more complex example where patterns are used a bit more.
- #!/bin/bash
- # Print a message about disk useage.
- space_free=$( df -h | awk '{ print $5 }' | sort -n | tail -n 1 | sed 's/%//' )
- case $space_free in
- [1-5]*)
- echo Plenty of disk space available
- ;;
- [6-7]*)
- echo There could be a problem in the near future
- ;;
- 8*)
- echo Maybe we should look at clearing out old files
- ;;
- 9*)
- echo We could have a serious problem on our hands soon
- ;;
- *)
- echo Something is not quite right here
- ;;
- esac
No comments:
Post a Comment