Last active
June 11, 2021 07:56
-
-
Save aejh/9dfc6886ab55aca5308f63eae3fc5c15 to your computer and use it in GitHub Desktop.
Shell script argument handling, quick n dirty
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
# For reference: | |
# $# The number of arguments | |
# $0 The program name | |
# $@ All arguments | |
# $1 .. $N Argument 1 .. N | |
while [ $# -gt 0 ]; do | |
# $1 contains next argument, process $1 here | |
echo $1 | |
shift # Pops $1 off the argument stack | |
done | |
# or using a case statement | |
while [ $# -gt 0 ]; do | |
case "$1" in | |
"-v") VERBOSE=true;; | |
# More here | |
*) echo "Unknown argument: $1"; exit 1;; | |
esac | |
shift | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment