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
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.
---------- 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.
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
---------- Post updated at 04:26 PM ---------- Previous update was at 04:23 PM ----------
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) :