1

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=

1 Answer 1

1

See man getopt. Basically the getopt which is part of macOS doesn't support long option names. Using

OPTS=`getopt f:l: $*`

works as expected.

PS: The man page also recommends to use $* instead of "$@" but this is not related to your problem.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .