How to use case and command line arguments in shell script?

Hi... can anyone please help me out in using the CASE and command line argument in shell script... i am bit new to shell scripting...below i have explained my proble with example...

say i have an executable file with name 'new1.sh' and there are 3 functions in it a(), b() and c()....and there is 1 function Options().. so the file looks like this...

new1.sh

#!bin/ksh/
a()
{
..
}
b()
{
..
}
c()
{..
}
 
options()
{
case:
a> sqlpackage "new.a"
b> sqlpackage "new.b"
c> sqlpackage "new.c"
}

now i want to use the above file using the below command format..

new1.sh -a -b -c

so that all the 3 functions are called by this command... and the user should have freedom to give as many options as he wishes to.... like he can give only 1 command line arg...

new1.sh -b

OR 2 arg like..

new1.sh -a -c

can anyone please help me in writing this code...

---------- Post updated at 11:50 PM ---------- Previous update was at 11:25 PM ----------

hi....
can anyone plesae help me in this quickly...
thanks in advance...

In bash...

#!/bin/sh

while getopts abc opt
do
case $opt in
a) echo sqlpackage "new.a";;
b) echo sqlpackage "new.b";;
c) echo sqlpackage "new.c";;
esac
done

Invocation

sh script.sh -a -b -c

hi...
i am getting the below error after trying with what you suggested...

new9[171]: getopts: 0403-010 A specified flag is not valid for this command.

also please let me know what does 'abc' in 'while getopts abc opt' means...
what if i have options such as 'n1', 'n2', 'n3'?? would the while loop become 'while getopts n1n2n3 opt'??

please note my options are not exactly a, b, c... i just gave them as an example...
my correct options are bl, bc, mm....

The "getopts" command assumes that you will be using standard unix command line syntax (like in your post #1).

What Operating System and version do you have?
What Shell are you using?

When you go the error message, what was in your shell script and what did you type to invoke the script?

The "abc" is the list of possible options (-a, -b , -c). The command line is parsed and each value is placed in turn into the environment variable $opt. If you supply three options on the command line the while loop executes three times (once for each option).
There is an extensive expanation in "man getopts".

The rest of you post means nothing to me.

From the error string I would say the OS is AIX and you have specified the same character more than once.

getopts only supports single character options so while getopts blbcmn opt won't work you should probably consider using 1 char for each option eg l c and m would give while getopts lcm opt

Otherwise you will need to code the option processing yourself, but in your case this is pretty simple as you only have flag options and something like this should work out fine:

#!/bin/sh
BL=0
BC=0
MM=0
while [ $# -gt 0 ]
do
    case $1 in
        -bl) BL=1 ;;
        -bc) BC=1 ;;
        -mm) MM=1 ;;
        -*) echo "$0: Invalid option $1" ; exit 2 ;;
        *) break ;;
    esac
    shift
done
[ $BL -eq 1 ] && echo "run the bl function"
[ $BC -eq 1 ] && echo "run the bc function"
[ $MM -eq 1 ] && echo "run the mm function"
1 Like

hey thanks man... it is working exactly as i wanted...

thanks all for your help...