| 1 | #!/bin/bash |
| 2 | |
| 3 | # Check some of the system's environmental variables. |
| 4 | # This is good preventative maintenance. |
| 5 | # If, for example, $USER, the name of the person at the console, is not set, |
| 6 | #+ the machine will not recognize you. |
| 7 | |
| 8 | : ${HOSTNAME?} ${USER?} ${HOME?} ${MAIL?} |
| 9 | echo |
| 10 | echo "Name of the machine is $HOSTNAME." |
| 11 | echo "You are $USER." |
| 12 | echo "Your home directory is $HOME." |
| 13 | echo "Your mail INBOX is located in $MAIL." |
| 14 | echo |
| 15 | echo "If you are reading this message," |
| 16 | echo "critical environmental variables have been set." |
| 17 | echo |
| 18 | echo |
| 19 | |
| 20 | # ------------------------------------------------------ |
| 21 | |
| 22 | # The ${variablename?} construction can also check |
| 23 | #+ for variables set within the script. |
| 24 | |
| 25 | ThisVariable=Value-of-ThisVariable |
| 26 | # Note, by the way, that string variables may be set |
| 27 | #+ to characters disallowed in their names. |
| 28 | : ${ThisVariable?} |
| 29 | echo "Value of ThisVariable is $ThisVariable". |
| 30 | |
| 31 | echo; echo |
| 32 | |
| 33 | |
| 34 | : ${ZZXy23AB?"ZZXy23AB has not been set."} |
| 35 | # Since ZZXy23AB has not been set, |
| 36 | #+ then the script terminates with an error message. |
| 37 | |
| 38 | # You can specify the error message. |
| 39 | # : ${variablename?"ERROR MESSAGE"} |
| 40 | |
| 41 | |
| 42 | # Same result with: dummy_variable=${ZZXy23AB?} |
| 43 | # dummy_variable=${ZZXy23AB?"ZXy23AB has not been set."} |
| 44 | # |
| 45 | # echo ${ZZXy23AB?} >/dev/null |
| 46 | |
| 47 | # Compare these methods of checking whether a variable has been set |
| 48 | #+ with "set -u" . . . |
| 49 | |
| 50 | |
| 51 | |
| 52 | echo "You will not see this message, because script already terminated." |
| 53 | |
| 54 | HERE=0 |
| 55 | exit $HERE # Will NOT exit here. |
| 56 | |
| 57 | # In fact, this script will return an exit status (echo $?) of 1. |