Problem in bash script

I have written a script and I get error and I don't understand why.

neededParameters=2
numOfParameters=0
correctNum=0
while getopts "s:l:" opt 
do
    case "$opt" in
    s)
        serviceName= $OPTARG #errorline 1 
        numOfParameters= $numOfParameters + 1
        ;;
    l)
        fileName= $OPTARG #errorline 2 
        numOfParameters= $numOfParameters + 1    
        ;;
    ?)
        echo "unknown option"
        ;;
    esac
done

when I run:
script -s s -l l
Then I get error message on errorlines 1 and 2 that it doesn't recognize s and l.

I would be really glad for an help.
Thanks in advance.

ok i tested this under linux, it runs fine

neededParameters=2
numOfParameters=0
correctNum=0
while getopts "s:l:" opt
do
    case $opt in
    s)
        serviceName=$OPTARG #errorline 1
        let numOfParameters++
        ;;
    l)
        fileName=$OPTARG #errorline 1
        let numOfParameters++
        ;;
    \?)
        echo "unknown option"
        ;;
    esac
done
1 Like

Modified version:

neededParameters=2
numOfParameters=0
correctNum=0
while getopts "s:l:" opt
do
    case $opt in         # the quotes are unnecessary in this context
    s)
        serviceName=$OPTARG # no space after the equal sign
        numOfParameters=$(( $numOfParameters + 1 )) # I suppose you need arithmetic expansion here
        ;;
    l)
        fileName=$OPTARG #errorline 2 
        numOfParameters=$(( $numOfParameters + 1 )) 
        ;;
    ?)
        echo "unknown option"
        ;;
    esac
done
1 Like

Sorry I could not use s (for there is a silly program with that name on this box (AIX)...)

an12:/home/vbe $ more bash_opt.sh
neededParameters=2
numOfParameters=0
correctNum=0
#while getopts "s:l:" opt 
while getopts t:l: opt 
do
    case "$opt" in
    t)
        serviceName=$OPTARG #errorline 1 
        numOfParameters=`expr $numOfParameters + 1`
        ;;
    l)
        fileName= $OPTARG #errorline 2 
        numOfParameters=`expr $numOfParameters + 1`
        ;;
    ?)
        echo "unknown option"
        ;;
    esac
done
echo neededParameters= $neededParameters
echo numOfParameters= $numOfParameters
an12:/home/vbe $ bash -debug bash_opt.sh -t t -l l
bash_opt.sh: line 13: l: command not found
neededParameters= 2
numOfParameters= 2
1 Like

O.K.

But why do you think that?

Because from what I understand when there is colons after the letter then it mean that a parameter must come after it when you run the script.
I mean it mean that if you write in the shell:
script -s
then the colons after the s (in the script) will send error.
Hope I explain myself good.

These two statements have different meanings.

Thanks I will check your suggestion.

---------- Post updated at 04:53 PM ---------- Previous update was at 04:46 PM ----------

numOfParameters=`expr $numOfParameters + 1`

Is this the way to do arithmetic in bash script.
using Quotation markand and exper.
Because I thought you can just write the variables and do use math operator.

Could also be written (in ksh and bash)

numOfParameters=$(($numOfParameters + 1))

Thanks you, you solved my problem.

Unfortunately I know nothing about bash (ksh user...) but know some posix...
As I just found a box (AIX 6.1) that seems to have bash I gave a try...
The only other syntax that seems to work is this one:

let numOfParameters="$numOfParameters + 1"

But again Im no bash specialist and dont know what the implementation I use is worth...

Yep i just had a doubt that's why i removed my post just after ...
so i just went back to the RTFM part ...

Colon following letter means : the letter option requires an argument
An initial colon prevent getopts from printing an error message when invalid option are given
:smiley:

---------- Post updated at 04:26 PM ---------- Previous update was at 04:23 PM ----------

For lazy ppl like me :

let numOfParameters++

:wink:

Thanks ctsgnb!
Could you explain how

let numOfParameters++

works?

I just tried also last posix way:

 numOfParameters=$((numOfParameters + 1))

Works also...

Not sure about what you mean by "how it works?" but I suppose let set the var to 0 if not initialized or amibguous (not numeric or empty) and then increment it by 1 (a different step could be defined with the i+=n notation this would do i="$i + n") where n is the numeric step of course.

So all the following are equivalent the first one is just shorter to write (and maybe less parse consuming)

let numOfParameters++
let numOfParameters+=1
let numOfParameters="$numOfParameters + 1"

I tested the following on a linux machine and it works fine with the different sh ksh bash (i don't know if it behaves the same way on AIX) :

$ ps
  PID TTY          TIME CMD
  387 pts/67   00:00:00 bash
13882 pts/67   00:00:00 sh
14052 pts/67   00:00:00 ps
$ echo $$
13882
$ echo $i
1
$ let i++
$ echo $i
2
$ exec /bin/ksh
$ ps
  PID TTY          TIME CMD
  387 pts/67   00:00:00 bash
13882 pts/67   00:00:00 ksh
14225 pts/67   00:00:00 ps
$ echo $$
13882
$ echo $i

$ let i++
$ echo $i
1
$ exec /bin/bash
$ ps
  PID TTY          TIME CMD
  387 pts/67   00:00:00 bash
13882 pts/67   00:00:00 bash
14420 pts/67   00:00:00 ps
$ echo $$
13882
$ echo $i
/etc/profile.d/which-2.sh
$ i=
$ echo $i

$ let i++
$ echo $i
1
$