Created
January 8, 2014 14:04
-
-
Save NeilRobbins/8317189 to your computer and use it in GitHub Desktop.
Some simple argument handling for use in shell scripts. HT to an answer on StackOverflow.
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/sh - | |
#argument defaults | |
x="foo" | |
y="bar" | |
# argument handling | |
while test $# -gt 0 | |
do | |
case "$1" in | |
-s) x=${2} | |
echo "x set to: " ${2} | |
;; | |
-p) y=${2} | |
echo "y set to: " ${2} | |
;; | |
-*) echo "bad option $1" | |
;; | |
*) #ignore | |
;; | |
esac | |
shift | |
done | |
# inform if defaults will be used | |
if [ "${x}" = "foo" ]; then | |
echo "No x supplied. Default x will be used: " ${x} | |
fi | |
if [ "${y}" = "bar" ]; then | |
echo "No y supplied. Default y will be used: " ${y} | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment