Add Options to Shell Scripts
Example 1 (Without arguments):
#!/bin/bash
while getopts ":a" opt; do
case $opt in
a)
echo "-a was triggered" >&2
;;
\?)
echo "Invalid option" >&2
exit 1
;;
esac
done
Example 2 (With arguments):
#!/bin/bash
while getopts ":o:" opt; do
case $opt in
o)
if [ "$OPTARG" = "true" ]; then
echo "Option -o trigged and argument is true..."
else
echo "Option -o trigged but argument is not true"
fi
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
: )
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done