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?

有帮助吗?

解决方案

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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top