Skip to content

Instantly share code, notes, and snippets.

@goaquin
Created April 5, 2017 19:58
Show Gist options
  • Save goaquin/9564c042743cba4d145e919f18c7c5ee to your computer and use it in GitHub Desktop.
Save goaquin/9564c042743cba4d145e919f18c7c5ee to your computer and use it in GitHub Desktop.
#!/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