Created
April 5, 2017 19:58
-
-
Save goaquin/9564c042743cba4d145e919f18c7c5ee to your computer and use it in GitHub Desktop.
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 | |
# What is wrong with this code? | |
# | |
#./test.sh -i foo | |
# output: "has value foo" , as expected | |
# | |
#./test.sh -i | |
# output: nothing , expected "don't have a value" | |
# | |
#./test.sh | |
# output: nothing , expected "no flag" | |
while [[ $# -gt 1 ]] | |
do | |
key=$1 | |
case $key in | |
-i|--ip) | |
if [ $2 ] ; then | |
echo "has value $2" | |
exit 1 | |
else | |
echo "don't have a value" | |
exit 1 | |
fi | |
shift | |
;; | |
*) | |
echo "no flag" | |
exit 1 | |
;; | |
esac | |
shift | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment