1

Im following this guide to using getopt.

#!/bin/bash
ALPHA=test
BETA=test
CHARLIE=test
DELTA=test

usage()
{
  echo "Usage: alphabet [ -a | --alpha ] [ -b | --beta ]
                        [ -c | --charlie CHARLIE ] 
                        [ -d | --delta   DELTA   ] filename(s)"
  exit 2
}

PARSED_ARGUMENTS=$(getopt -a -n alphabet -o a:b:c:d: --long alpha:,bravo:,charlie:,delta: -- "$@")
VALID_ARGUMENTS=$?
if [ "$VALID_ARGUMENTS" != "0" ]; then
  usage
fi

 eval set -- "$PARSED_ARGUMENTS"
while :
do
  case "$1" in
    -a | --alpha)   ALPHA="$2"      ; shift 2   ;;
    -b | --beta)    BETA="$2"    ; shift  2 ;;
    -c | --charlie) CHARLIE="$2" ; shift 2 ;;
    -d | --delta)   DELTA="$2"   ; shift 2 ;;
     --) shift; break ;;
     *) echo "Unexpected option: $1 - this should not happen."
       usage ;;
  esac
done

echo "ALPHA   : $ALPHA"
echo "BETA    : $BETA "
echo "CHARLIE : $CHARLIE"
echo "DELTA   : $DELTA"
echo "Parameters remaining are: $@"

So all of the arguments are mandatory, if we didn't give the argument then it'll return the default value (where I have assigned from the beginning)

EX:

./script
PARSED_ARGUMENTS is  --
ALPHA   : unset
BETA    : unset
CHARLIE : unset
DELTA   : unset

Ex2

./script -a aaa
PARSED_ARGUMENTS is  -a 'aaa' --
ALPHA   : aaa
BETA    : unset
CHARLIE : unset
DELTA   : unset
Parameters remaining are:

Till here it is perfect.

Issue:

Now, if Im calling the argument but not giving any values while triggering the script, then its throwing error.

./script -a
alphabet: option requires an argument -- 'a'
Usage: alphabet [ -a | --alpha ] [ -b | --beta ]
                        [ -c | --charlie CHARLIE ]
                        [ -d | --delta   DELTA   ] filename(s)

In this case, if I didn't give the values for -a then it should take the value from the default.(from the beginning)

13
  • 1
    It's very rare that a utility behaves the way that you describe. If the user is giving the -a option, they do so to change its default value. Not giving an argument to -a should therefore be an error. Also your use of unset is a bit confusing. It should really say default. A variable with the value unset is clearly set to that value.
    – Kusalananda
    Apr 13, 2020 at 12:47
  • Oh its just copied from the site I referred, Its a default value, like 'test', I have updated the question
    – TheDataGuy
    Apr 13, 2020 at 12:51
  • Yes, but the code at the end of the link does not have option-arguments to some of its options. (you have a:b:c:d:, they have another string).
    – Kusalananda
    Apr 13, 2020 at 12:52
  • If i remove the : then ./script -a aaa this is not working.
    – TheDataGuy
    Apr 13, 2020 at 12:54
  • Well, you have to make up your mind. Should -a take an argument or not? Because you can't do both.
    – Kusalananda
    Apr 13, 2020 at 12:57

1 Answer 1

1

You cannot just give "-a" and leave it without argument rather you just don't mention "-a" at all and inside the script if -a option is not given , you can assign a default value like below:

if [[ -z $ALPHA ]]
then
ALPHA="defaultvalue"
fi

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .