문제

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.

도움이 되었습니까?

해결책 2

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++))
    ;;

다른 팁

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
    ;;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top