I am trying to write a program (using shell script or bash script) which accept command line argument using getopt.
It is working fine on Linux terminal but when I am using it on OS X Terminal it is not able to read the provided command line argument.
a sample code :
OPTS=`getopt -o f:l: --long FirstName:,LastName: -n 'parse-options' -- "$@"`
if [ $? != 0 ] ; then echo "Failed parsing options." >&2 ; exit 1 ; fi
echo "$OPTS"
eval set -- "$OPTS"
NEWLINE=$'\n'
while true; do
case "$1" in
-f | --FirstName ) FirstName="$2" ; shift 2 ;;
-l | --LastName ) LastName="$2" ; shift 2 ;;
-- ) shift; break ;;
* ) break ;;
esac
done
echo "${NEWLINE}"
echo "--------------------------------------------"
echo "FirstName=$FirstName"
echo "LastName=$LastName"
echo "${NEWLINE}"
sleep 1
Input :
sh try.sh --FirstName foor --LastName bar
Output on Linux (FC19):
--------------------------------------------
FirstName=foor
LastName=bar
Output on Mac :
--------------------------------------------
FirstName=
LastName=