Skip to content

Instantly share code, notes, and snippets.

@davidreuss
Created April 15, 2009 11:53
Show Gist options
  • Save davidreuss/95730 to your computer and use it in GitHub Desktop.
Save davidreuss/95730 to your computer and use it in GitHub Desktop.
Template for shell-script with option-parsing
#!/bin/bash
usage() {
echo >&2 "Usage: $0 [ -d directory | --dir directory ] [ -u user | --user user ] | --help"
}
invalid() {
echo >&2 "Invalid option $1. Try $0 --help to see available options."
}
while :
do
case $# in 0) break ;; esac
case $1 in
-d|--dir) shift; dir=$1; shift ;;
-u|--user) shift; user=$1; shift ;;
-a|--all) shift; all=true ;;
-|--) shift; break;;
-h|--help) usage ; exit 1;;
-*) invalid "$1"; exit 2 ;;
*) break ;;
esac
done
echo $user
echo $dir
echo "$@"
echo "$all"
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment