Saturday, 28 September 2013

Bash: getopts function

Bash: getopts function

# Correct usage
function usage() {
echo "Usage: $0 [-i] command [-t <i|w|e|se>] message" 1>&2; exit 1;
}
# There's an optional "i" parameter and an optional parameter "t" with an
argument
while getopts "it:" opt; do
case "${opt}" in
i)
installing=true
;;
t)
s="${OPTARG}"
if [ "$s" == "i" -o "$s" == "w" -o "$s" == "e" -o "$s" == "se" ]
then
messageType=$s
else
usage
exit 1
fi
;;
esac
done
echo "$OPTIND"
shift $(( OPTIND-1 ))
command="$1"
message="$2"
The problem here (among others) is when I call the script like this:
./script.sh oneCommand -t oneMessage
Because it interprets that the message is -t instead of oneMessage.

No comments:

Post a Comment