I am trying to make a shell script which is designed to be run like this:
script.sh -t application
Firstly, in my script I want to check to see
Use $#
to grab the number of arguments, if it is unequal to 2 there are not enough arguments provided:
if [ $# -ne 2 ]; then
usage;
fi
Next, check if $1
equals -t
, otherwise an unknown flag was used:
if [ "$1" != "-t" ]; then
usage;
fi
Finally store $2
in FLAG
:
FLAG=$2
Note: usage()
is some function showing the syntax. For example:
function usage {
cat << EOF
Usage: script.sh -t
Performs some activity
EOF
exit 1
}