2

I'm calling a bash script with the following arguments:

myscript.sh -d /tmp -e dev -id 12345 -payload /tmp/test.payload

and inside the script, would like to get the value for the -payload. I don't really care about the other arguments, but they will be present in the call.

Here's some code that almost works on retrieving the argument:

while getopts "d:e:payload:id:" arg; do
  case $arg in
    payload)
        echo "payload"
        ;;
  esac
done

Of course payload) in the case control structure doesn't work, so how can I grab the value for -payload and assign it to a variable?

1
  • getopts (a program, as opposed to the getopt command) supports long flags (--foo), though I think not on OSX Commented May 15, 2014 at 19:56

1 Answer 1

2

i not sure if this is the best way to handle it... but check these marked lines in a script

in your case i'd use

while test $# -gt 0; do
  case "$1" in
    -payload)
      shift
      PAYLOAD=$1
      ;;
     *)
     # Catch other parameters here
     # this part is not relevant 
     # to the answer but I added it
     # to avoid infinite loop mentioned
     shift
     ;;
  esac
done
Sign up to request clarification or add additional context in comments.

2 Comments

Shouldn't you add a shift after esac? Otherwise you get an infinite loop as unmatched parameters aren't consumed.
I edited the answer... I only added the relavent part that answer the question... Thanks for mentioning this @Martin

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.