1

I have a shell script where i am parsing command line argument. My arguments has path in it. Example: mycript.sh -c test -s global -n /mydir/test1 -d /orgdir/test35 When I run the script and echo the arguments which contains path(special char "/"), it gives me empty path.

#/!bin/bash
...

while getopts c:hs:n:d opt
do
case "$opt" in
c) INST=$OPTARG;;
d) INST_DIR=$OPTARG;;
h) usage;;
s) METHOD=$OPTARG;;
n) MAINTENANCE_DIR=$OPTARG;;
\?) usage;;
esac
done

echo INST dir is [$INST_DIR]
echo MAINTENANCE dir is [$MAINTENANCE_DIR]
.......

Result of this echo is

INST dir is []
MAINTENANCE dir is []

Can someone tell what is incorrect here?

2
  • it's not a direct solution to your problem, but I'd advice you to have a look at docopt which is a portable (across languages) way to parse arguments. Commented Feb 26, 2014 at 11:04
  • docopt is still a 3rd-party solution, though. getopts is part of bash, and so is completely portable (given the assumption that bash is available, anyway). Commented Feb 26, 2014 at 14:26

1 Answer 1

2

With your script, I have a different result:

INST dir is []
MAINTENANCE dir is [/mydir/test1]

I have changed line 4 of your script like this:

while getopts c:h:s:n:d: opt

It works fine for me under Cygwin, with bash version 4.1.10(4)-release.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 Same here. The only apparent problem in the original is the missing : after d in the option list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.