Created
April 15, 2009 11:53
-
-
Save davidreuss/95730 to your computer and use it in GitHub Desktop.
Template for shell-script with option-parsing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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