You seemed to have missed printing the message but rather passing the whole string as a command to run. Add an echo
before the string
case "$o" in
h )
echo "Usage:
sh $(basename "$0") -h Displays help message
sh $(basename "$0") arg Outputs ...
where:
-h help option
arg argument."
exit 0
;;
But generally prefer a style of adding a heredoc to printing out the multi-line string as
show_help() {
cat <<'EOF'
Usage:
sh $(basename "$0") -h Displays help message
sh $(basename "$0") arg Outputs ...
where:
-h help option
arg argument.
EOF
}
and use the function show_help
for the -h
flag.
Also for empty argument flags, the first calls to getopts()
exits the loop, so you cannot have a handle inside the loop. Have a generic check for empty arguments before invoking getopts()
if [ "$#" -eq 0 ]; then
printf 'no argument flags provided\n' >&2
exit 1
fi
With your earlier definition of argument flag :h
, suggests that -h
does not take any argument. The clause :)
only applies when you define -h
to take an argument i.e. when defined as :h:
. Only then you run it without passing the arguments the code under :)
gets executed. Putting together the whole script
#!/usr/bin/env bash
if [ "$#" -eq 0 ]; then
printf 'no argument flags provided\n' >&2
exit 1
fi
show_help() {
cat <<'EOF'
Usage:
sh $(basename "$0") -h Displays help message
sh $(basename "$0") arg Outputs ...
where:
-h help option
arg argument.
EOF
}
while getopts ":h:" opt; do
case "$opt" in
h )
show_help
exit 1
;;
\? )
echo "Invalid option -$OPTARG" 1>&2
exit 1
;;
: )
echo "Invalid option -$OPTARG requires argument" 1>&2
exit 1
;;
esac
done
and running it now
$ bash script.sh
no argument flags provided
$ bash script.sh -h
Invalid option -h requires argument
$ bash script.sh -s
Invalid option -s