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=
Best Answer
See
man getopt
. Basically thegetopt
which is part of macOS doesn't support long option names. Usingworks as expected.
PS: The man page also recommends to use
$*
instead of"$@"
but this is not related to your problem.