BarneyL. BarStin
BarneyL. BarStin

Reputation: 343

Take param in bash

I need take params in bash, I can take the param h,c,p,s,a but i cant take the param b.

Why can't I take it? This is my script:

if [ ! -z $1 ]; then
    HOSTNAME=""
    CLIENT=""
    SUBSCRIPTIONS_GROUPS=""
    PROVIDER=""
    SERVER=""
    while getopts ":h:c:p:s:a:b" opt; do
            case $opt in 
                    h)  HOSTNAME=${OPTARG}
                            ;;
                    c)  CLIENT=${OPTARG}
                            ;;
                    p)      PROVIDER=${OPTARG}
                            ;;
                    s)  SUBSCRIPTIONS_GROUPS=${OPTARG}
                            ;;
                    a)  ALWAYS_ON="on"
                            ;;
                    b)  SERVER=${OPTARG}
                            ;;
                    ?)  ;;
            esac
    done
fi

Upvotes: 0

Views: 105

Answers (3)

bro
bro

Reputation: 771

I see two flaws in your implementation:

  • First character is colon
  • After b there is no colon

Your getops string should look like this:

while getopts "h:c:p:s:ab:" opt; do
...

When you want getopts to expect an argument for an option, just place a : (colon) after the proper option flag

and

If the very first character of the option-string is a : (colon), which would normally be nonsense because there's no option letter preceding it, getopts switches to "silent error reporting mode". In productive scripts, this is usually what you want because it allows you to handle errors yourself without being disturbed by annoying messages.

Extracts from http://wiki.bash-hackers.org/howto/getopts_tutorial

Upvotes: 2

rghome
rghome

Reputation: 8819

From the manual:

If a character is followed by a colon, the option is expected to have an argument, which should be supplied as a separate argument.

So b needs to be followed by a colon.

Upvotes: 0

Bernhard
Bernhard

Reputation: 8831

Your are missing the ':' after your parameter 'b'. Also 'a' looks like a boolean flag, so no ':' in such a case.

Upvotes: 0

Related Questions