0

I have a bash script with command line params that may look like this:

./myscript.sh -P1 valueofP1 -A3 valueofA3

is there any common method of getting the values shown here and having local script variables P1 and A3 get the value that follows in the next parameter?

JasonGenX
  • 4,952
  • 27
  • 106
  • 198
  • Comment by RM1970 is right on. Check out http://stackoverflow.com/questions/16483119/example-of-how-to-use-getopt-in-bash. – Skurfur Nov 20 '13 at 17:01

1 Answers1

1

shift

while [ $# -gt 0 ]
do
    case $1 in
    -P1)
        shift
        arg=$1
        shift
        echo "P1:$arg"
        ;;
    -A3)
        shift
        arg1=$1
        shift
        arg2=$1
        shift
        echo "A3: $arg1 and $arg2"
        ;;
    *)
        echo "Unknown argument $1" >&2
        exit 1
        ;;
    esac
done

usage:

$ ./test.sh -P1 AAA -A3 B C
P1:AAA
A3: B and C
Sergey Fedorov
  • 2,169
  • 1
  • 15
  • 26