Skip to content

Instantly share code, notes, and snippets.

@shelling
Created February 16, 2012 02:35
Show Gist options
  • Select an option

  • Save shelling/1841125 to your computer and use it in GitHub Desktop.

Select an option

Save shelling/1841125 to your computer and use it in GitHub Desktop.
template of getopt in shell
#!/bin/bash
qamode="false"
action=$1
shift
case "$action" in
start|stop)
;;
*)
echo "Usage: $0 {start|stop} [options]"
exit 2
;;
esac
options=$(getopt -o a:qh -l append,qamode,help -- $*)
if [ $? != 0 ]; then
exit 2;
fi
set -- $options
while true
do
case $1 in
-a|--append)
echo append arg $2
# $1 is -a | --append now
# $2 is the argument of -a now
shift
;;
-q|--qamode)
qamode="true"
;;
-h|--help)
echo "help"
exit 0
;;
*)
break;
;;
esac
shift
done
if [[ "true" == $qamode ]]; then
echo "qa mode was enabled"
else
echo "no qa mode"
fi
echo "done"
case $action in
start)
echo "start process"
;;
stop)
echo "stop process"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment