0

Already asked question related this few days ago here

But this time the condition different,Having following bash script using getopts

#!/bin/bash
ipaddr=""
sysip=""
msgType=""
sshTimeout=""
bulbIndex=""
bulbstate=""
while getopts ":ht:d:A:r:s:m:" OPTION
do
    case $OPTION in
        h)
            usage $LINENO
            ;;

        t)
            let "t_count += 1"
            ipaddr="${OPTARG}"
            echo -e $ipaddr
            ;;

        d)
            let "d_count += 1"
            echo "Not supported"
            exit 0
            ;;

        A)
            let "A_count += 1"
            bulbIndex="${OPTARG}"  #  After -A option firsr argument is bulb index and second is state off/on
            bulbstate=$3
            printf "Set %s state on %s bulb\n" $bulbstate $bulbIndex
            ;;

        r)
            let "r_count += 1"
            sysip="${OPTARG}"
            echo -e $sysip
            ;;

        m)
            let "m_count += 1"     #message type 1:text 2:number 3:Text&number
            msgType="${OPTARG}"
            echo -e $msgType
            ;;

        s)
            let "s_count += 1"
            sshTimeout="${OPTARG}"
            echo -e $sshTimeout
            ;;

        ?)
            echo -e "wrong command sysntax"
            exit 0
            ;; 
    esac
done

Above script working fine for all options except -A option.What is wrong with it let you know from below script execution steps

$ ./sample.bash -A 3 OFF
Set OFF state on 3 bulb

This is expected output but when i give multiple option then it behave wrong like

$ ./sample.bash -t 192.168.0.1 -r 192.169.0.33 -A 3 OFF
192.168.0.1
192.169.0.33
Set -r state on 3 bulb

Here i expect OFF instead -r and obviously it gives this output because this time it not $3 but it $7 but my problem is how i inform to script it's now $7 not $3.

And

$ ./sample.bash -t 192.168.0.1 -A 3 OFF -r 192.169.0.33 -m 1
192.168.0.1
Set -A state on 3 bulb

this time after -A all options are discarded and again -A instead OFF

How can i correctly access both arguments after -A option in any sequence of -A options?

Also any one have query regarding question let me know and frankly speaking whatever solution of it means very simple or hard but currently i don't know.

Community
  • 1
  • 1
Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73

2 Answers2

0

getopts only accepts one argument per option. How about passing both arguments to -A inside quotes, and separating them later inside your case statement?

A)
    let "A_count += 1"
    bulbIndex=${OPTARG% O*}
    bulbstate=${OPTARG#* }
    printf "Set %s state on %s bulb\n" $bulbstate $bulbIndex
    ;;

Then calling using:

$ ./sample.bash -t 192.168.0.1 -r 192.169.0.33 -A "3 OFF"

Gives:

192.168.0.1
192.169.0.33
Set OFF state on 3 bulb

If you can't use the quotes, then if you can make sure that -A is the last option used, then you can use the OPTARG to get the number, then simply get the final argument separately.

A)
    let "A_count += 1"
    bulbIndex="${OPTARG}"
    bulbstate=${@: -1}
    printf "Set %s state on %s bulb\n" $bulbstate $bulbIndex
    ;;
Josh Jolly
  • 11,258
  • 2
  • 39
  • 55
  • Thanks for answer, you are right but it must for me to use `-A 3 4` instead yours – Jayesh Bhoi Mar 04 '14 at 12:35
  • Not sure what you mean, are you saying that you should be able to use -A with any number of bulbIndex arguments? eg `-A 3 4 5 OFF` ? – Josh Jolly Mar 04 '14 at 12:42
  • Not, and my mistake in previous comment it's `-A 3 OFF` not `-A 3 4` .must only two argument after `-A` option. – Jayesh Bhoi Mar 04 '14 at 12:48
  • Added another way to do it - but it only works if -A is the last option used. As I said in the answer, getopts only accepts one argument for each option, so adding a second argument is by necessity a bit of a hack. Alternatively, how about having a second argument? eg -A for ON, -X for OFF or similar. – Josh Jolly Mar 04 '14 at 12:54
  • That is the way to do but must `-A` is last option and in my case it's not case. Also some playing `OPTIND` it may be possible to get it.on my side have still try to get it and Thanks friend for stick with my question to solve it. – Jayesh Bhoi Mar 04 '14 at 13:01
0

Finally after playing with OPTIND have found the way to get it.Modified the -A option as follows in getopts

A)
    let "A_count += 1"
    let "index += $OPTIND" # where initial value of index is 2

    bulbstate=${!index}

    bulbIndex="${OPTARG}"

    printf "Set %s state on %s bulb\n" $bulbstate $bulbIndex

    ((OPTIND++))
    ;;
Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73