Created
April 17, 2014 07:03
-
-
Save RichardBronosky/10959479 to your computer and use it in GitHub Desktop.
I created this gist to demo this stackexchange answer: http://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash/14203146#14203146
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 | |
for i in "$@" | |
do | |
case $i in | |
-e=*|--extension=*) | |
EXTENSION="${i#*=}" | |
shift | |
;; | |
-s=*|--searchpath=*) | |
SEARCHPATH="${i#*=}" | |
shift | |
;; | |
-l=*|--lib=*) | |
LIBPATH="${i#*=}" | |
shift | |
;; | |
--default) | |
DEFAULT=YES | |
shift | |
;; | |
*) | |
# unknown option | |
;; | |
esac | |
done | |
echo "FILE EXTENSION = ${EXTENSION}" | |
echo "SEARCH PATH = ${SEARCHPATH}" | |
echo "LIBRARY PATH = ${LIBPATH}" | |
echo "Number files in SEARCH PATH with EXTENSION:" $(ls -1 "${SEARCHPATH}"/*."${EXTENSION}" | wc -l) | |
if [[ -n $1 ]]; then | |
echo "Last line of file specified as non-opt/last argument:" | |
tail -1 $1 | |
fi |
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 | |
while [[ $# > 1 ]] | |
do | |
key="$1" | |
shift | |
case $key in | |
-e|--extension) | |
EXTENSION="$1" | |
shift | |
;; | |
-s|--searchpath) | |
SEARCHPATH="$1" | |
shift | |
;; | |
-l|--lib) | |
LIBPATH="$1" | |
shift | |
;; | |
--default) | |
DEFAULT=YES | |
shift | |
;; | |
*) | |
# unknown option | |
;; | |
esac | |
done | |
echo FILE EXTENSION = "${EXTENSION}" | |
echo SEARCH PATH = "${SEARCHPATH}" | |
echo LIBRARY PATH = "${LIBPATH}" | |
echo "Number files in SEARCH PATH with EXTENSION:" $(ls -1 "${SEARCHPATH}"/*."${EXTENSION}" | wc -l) | |
if [[ -n $1 ]]; then | |
echo "Last line of file specified as non-opt/last argument:" | |
tail -1 $1 | |
fi |
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
chmod +x space_separated.sh | |
chmod +x equals_separated.sh | |
echo Testing space_separated.sh | |
./space_separated.sh -e conf -s /etc -l /usr/lib /etc/hosts | |
echo Testing equals_separated.sh | |
./equals_separated.sh -e conf -s /etc -l /usr/lib /etc/hosts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment