Database Reference
In-Depth Information
Values identified by a character followed by a colon “ : ” in the getopts statement indicate that the flag will be
followed by a string. An OptValue without a colon won't accept a string and can be used to signal a particular process
in your script.
In the example below, if the -t flag is received on the command line, a function named RunTestFunctions is executed:
change_admin_depts.sh -a 747 -b 757 -t
The script handles each of these requests in order:
while getopts a:b:t OptValue; do
case $OptValue in
a) export OLD_DEPT=${OPTARG};;
b) export NEW_DEPT=${OPTARG};;
t) export TestOnly="True"
*) echo "No values were provided for old and new departments";;
esac
done
if [ $TestOnly == True ]; then
RunTestFunctions
fi
The flexibility of this approach grows with the number of values you can handle. For instance, the logic in the
ChangeAdminDepts function could be adapted to process the other values you can set with the modify_user verb, like
line of business and location. The getopts flags could be used to determine which of those functions to apply. See the
example below:
while getopts a:b:t OptValue; do
case $OptValue in
a) export OLD_DEPT=${OPTARG};;
b) export NEW_DEPT=${OPTARG};;
c) export OLD_LOB=${OPTARG};;
d) export NEW_LOB=${OPTARG};;
e) export OLD_LOCATION=${OPTARG};;
f) export NEW_LOCATION=${OPTARG};;
t) export TestOnly="True"
*) echo "No values were provided for old and new departments";;
esac
done
if [ ${#OLD_DEPT} -gt 0 ]; then
if [ ${#NEW_DEPT} -gt 0 ]; then
ChangeAdminDepts
else
echo "Error: You provided the old department but"
echo "the value for the new department is missing."
echo "Please run this script again and provide"
echo "both values when prompted"
exit 1
fi
fi
Search WWH ::




Custom Search