Skip to content

Instantly share code, notes, and snippets.

@asears
Created January 4, 2016 17:34
Show Gist options
  • Save asears/839ac3ebcc18e7eae51f to your computer and use it in GitHub Desktop.
Save asears/839ac3ebcc18e7eae51f to your computer and use it in GitHub Desktop.
#!/bin/bash
showopts () {
while getopts ":pq:" optname
do
case "$optname" in
"p")
echo "Option $optname is specified"
;;
"q")
echo "Option $optname has value $OPTARG"
;;
"?")
echo "Unknown option $OPTARG"
;;
":")
echo "No argument value for option $OPTARG"
;;
*)
# Should not occur
echo "Unknown error while processing options"
;;
esac
done
return $OPTIND
}
showargs () {
for p in "$@"
do
echo "[$p]"
done
}
optinfo=$(showopts "$@")
argstart=$?
arginfo=$(showargs "${@:$argstart}")
echo "Arguments are:"
echo "$arginfo"
echo "Options are:"
echo "$optinfo"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment