LDP/LDP/guide/docbook/abs-guide/ex6.sh

58 lines
1.5 KiB
Bash
Raw Normal View History

2001-07-10 14:25:50 +00:00
#!/bin/bash
2004-04-28 12:08:07 +00:00
# Check some of the system's environmental variables.
# This is good preventative maintenance.
2001-10-15 14:21:41 +00:00
# If, for example, $USER, the name of the person at the console, is not set,
#+ the machine will not recognize you.
2001-07-10 14:25:50 +00:00
: ${HOSTNAME?} ${USER?} ${HOME?} ${MAIL?}
echo
echo "Name of the machine is $HOSTNAME."
echo "You are $USER."
echo "Your home directory is $HOME."
echo "Your mail INBOX is located in $MAIL."
echo
echo "If you are reading this message,"
echo "critical environmental variables have been set."
echo
echo
# ------------------------------------------------------
2001-10-15 14:21:41 +00:00
# The ${variablename?} construction can also check
#+ for variables set within the script.
2001-07-10 14:25:50 +00:00
ThisVariable=Value-of-ThisVariable
2001-10-15 14:21:41 +00:00
# Note, by the way, that string variables may be set
#+ to characters disallowed in their names.
2001-07-10 14:25:50 +00:00
: ${ThisVariable?}
echo "Value of ThisVariable is $ThisVariable".
2008-11-23 22:43:47 +00:00
echo; echo
2001-07-10 14:25:50 +00:00
: ${ZZXy23AB?"ZZXy23AB has not been set."}
2008-11-23 22:43:47 +00:00
# Since ZZXy23AB has not been set,
2001-10-15 14:21:41 +00:00
#+ then the script terminates with an error message.
2001-07-10 14:25:50 +00:00
# You can specify the error message.
2005-03-21 13:51:11 +00:00
# : ${variablename?"ERROR MESSAGE"}
2001-07-10 14:25:50 +00:00
2008-11-23 22:43:47 +00:00
# Same result with: dummy_variable=${ZZXy23AB?}
# dummy_variable=${ZZXy23AB?"ZXy23AB has not been set."}
2001-07-10 14:25:50 +00:00
#
2008-11-23 22:43:47 +00:00
# echo ${ZZXy23AB?} >/dev/null
2001-07-10 14:25:50 +00:00
2004-04-28 12:08:07 +00:00
# Compare these methods of checking whether a variable has been set
#+ with "set -u" . . .
2001-07-10 14:25:50 +00:00
2004-04-28 12:08:07 +00:00
echo "You will not see this message, because script already terminated."
2001-07-10 14:25:50 +00:00
HERE=0
2004-04-28 12:08:07 +00:00
exit $HERE # Will NOT exit here.
2005-03-21 13:51:11 +00:00
# In fact, this script will return an exit status (echo $?) of 1.