0

Does anyone know why it doesnt do anything?

moznostm=
moznostl=
moznosta=
moznostb=
while getopts ":m:l:b:a:h:" OPTION
do
case $OPTION in
m)
moznostm=$OPTARG
echo "bol zadany argument $moznost"
;;
l)
moznostl=$OPTARG
echo bol zadany argument $OPTARG
;;
b)
moznostb=$OPTARG
echo bol zadany argument $OPTARG
;;
a)
moznosta=$OPTARG
echo bol zadany argument $OPTARG
;;
h)
echo prazny
;;
esac
done
0

Just a few modifications.

1) Set your shebang! #!/bin/bash if you write bash

2) Reset if getopts was used previously

3) You forgot an m at $moznost.

4) Don't use uppercase for normal variable names.

5) Use more quotes. Double quotes " " when expanding variables.
6) Use shift.

7) You don't have to define / initialize variables like you are doing here moznostm= .

#!/bin/bash

OPTIND=1

    moznostm=
    moznostl=
    moznosta=
    moznostb=
    while getopts "mlbah:" opt
    do
    case "$opt" in
    m)
    moznostm=$OPTARG
    echo "bol zadany argument $moznostm"
    ;;
    l)
    moznostl=$OPTARG
    echo "bol zadany argument $OPTARG"
    ;;
    b)
    moznostb=$OPTARG
    echo "bol zadany argument $OPTARG"
    ;;
    a)
    moznosta=$OPTARG
    echo "bol zadany argument $OPTARG"
    ;;
    h)
    echo "prazny"
    ;;
    esac
    done
shift "$((OPTIND-1))"

Execute the script:

chmod +x scriptname && ./scripname -m
  • What do you mean by point "7"? Certainly you don't want to use a variable uninitialised (especially when considering that shells inherit variables from the environment). – Stéphane Chazelas Mar 19 '15 at 15:09
  • By point 7, I mean that moznostm= seems to be pointless. If it was a variable holding an integer, then there are situations a variable should be initialized as 0 so you could start counting or do other arithmetic calculations. In the setting as it is now, I don't see how moznostm= is going to help. – Valentin Bajrami Mar 19 '15 at 15:20
  • moznostm= would set the default value of $moznostm to the empty string as opposed to what value it got from the environment. You'd need m: in the option specification btw if you're going to use $OPTARG. – Stéphane Chazelas Mar 19 '15 at 15:29
  • Well I'm not really sure if I understand the purpose though. Feel free to improve the answer and provide some examples which demonstrates the caveats – Valentin Bajrami Mar 19 '15 at 15:44
  • A few additional remarks on the proposed solution. ad 1) The code snippet would also work with ksh. ad 5) Some variable expansion contexts don't require quoting; as the assignment was left unquoted, also the case argument (or the shift argument) don't require quoting. ad 6) To clarify; shift is only necessary if you have further arguments to be processed. – Janis Mar 19 '15 at 16:05
1

You have to call your script with an option and an option-argument. For example:

bash yourscript -l argumentfor_l

Note also that moznost is printed but never defined.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.