I made a script to restart my web server automatically, do certain checks, and then print a message with the time stamp to a log file. Since I call this file from different places (cron jobs, when I update my site, manually, etc.), I wanted to have a way to print different messages to the log file based on the reason why I'm calling it. I decided to use options to do this, and I also decided to add help, verbose, and test options as well.
Code for the options part:
# initializing variables
test=false
help=false
verbose=false
modeCount=0
msg="restarted"
# looking at options
while getopts ":aeghmstv" option; do
case $option in
a) # Automatic restart
((modeCount+=1))
msg="$msg automatically";;
e) # Error-triggered restart
((modeCount+=1))
msg="$msg due to an error";;
g) # Pull-triggered restart
((modeCount+=1))
msg="$msg on git pull";;
h) # Help
help=true;;
m) # Manual restart
((modeCount+=1))
msg="$msg manually";;
s) # Startup-triggered restart
((modeCount+=1))
msg="$msg on startup";;
t) # Testing mode
test=true;;
v) # Verbose mode
verbose=true;;
\?) # Invalid option
echo "Error: Invalid option; use the h option for help"
exit;;
esac
done
# checking for input errors
if [ "$help" == true ]; then
if [ $modeCount -gt 0 ] || [ "$test" == true ] || [ "$verbose" == true ]; then
echo "Error: No other options can be used with h; use the h option for help"
else
help
fi
exit
fi
if [ $modeCount != 1 ]; then
echo "Error: 1 log message option must be used; use the h option for help"
exit
fi
But, additionally, I want to be able to pass a string as a positional parameter to add additional information to my log file. For example, if I run:
./restart.sh -a
It logs something like:
2021-10-04T00:00:04 restarted automatically
But I want to be able to change that so that I could (optionally) run:
./restart.sh -a "daily restart"
And it would instead log:
2021-10-04T00:00:04 restarted automatically: daily restart
I found this question about mixing getops and parameters, but I do not know how I would do this if I want the parameters to be optional.
Once I get that string, it will be very easy to simply add a line like:
msg="$msg: $info"
But I am not sure how I would verify that such a parameter exists and then subsequently store it in a variable.
PS: I would also like it to work no matter what order the parameters/options are in. For example, I want:
./restart.sh -a "daily restart"
and
./restart.sh "daily restart" -a
to work the same way.