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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use case and command line arguments in shell script?
# 1  
Old 03-22-2011
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
Code:
#!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..
Code:
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...
Code:
new1.sh -b

OR 2 arg like..
Code:
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...

Last edited by Scott; 03-23-2011 at 03:49 PM.. Reason: Code tags, please...
This User Gave Thanks to swap21783 For This Post:
# 2  
Old 03-22-2011
In bash...
Code:
#!/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
Code:
sh script.sh -a -b -c

# 3  
Old 03-22-2011
hi...
i am getting the below error after trying with what you suggested...
Code:
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....

Last edited by Scott; 03-23-2011 at 03:51 PM.. Reason: Code tags
# 4  
Old 03-22-2011
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?

Quote:
also please let me know what does 'abc' in 'while getopts abc opt' means...
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.
# 5  
Old 03-22-2011
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:

Code:
#!/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"


Last edited by Chubler_XL; 03-22-2011 at 07:37 PM..
This User Gave Thanks to Chubler_XL For This Post:
# 6  
Old 03-23-2011
MySQL

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

thanks all for your help...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Passing arguments from command line to switch case statement in C

Hi Am pretty new to C.. Am trying to pass the arguments from command line and use them in switch case statement.. i have tried the following #include <stdlib.h> main(int argc, char* argv) { int num=0; if ( argc == 2 ) num = argv; printf("%d is the num value",num); switch ( num ) ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

2. Shell Programming and Scripting

Parsing Command Line Arguments In C shell script

]I have a string like "/abc/cmind/def/pq/IC.2.4.6_main.64b/lnx86" and this string is given by user. But in this string instead of 64b user may passed 32 b an i need to parse this string and check wether its is 32b or 64 b and according to it i want to set appropriate flags. How will i do this... (11 Replies)
Discussion started by: codecatcher
11 Replies

3. UNIX for Dummies Questions & Answers

Script to iterate all command line arguments

Hi guys, I am having trouble with this script. What i want it to do is to iterate all command line arguments in reverse order. The code below does this fine but i need the output to print the words on separate lines instead of one line: #!/bin/bash #Takes in the arguments and displays them... (7 Replies)
Discussion started by: pipeline2012
7 Replies

4. Programming

How to pass the command line arguments to the shell script in c language?

hi, I am new in the shell script, and c programming with linux. I am looking to pass the arguments in c program that should be executed by the shell script. e.g. #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv) { int i; for (i=1;i<argc; i++) { ... (2 Replies)
Discussion started by: sharlin
2 Replies

5. Shell Programming and Scripting

Shell script command line arguments

Hello All, i am known to the limitation of different shells while passing more than 9 command line arguments i just tried the example below i do see my current shell is tcsh echo $SHELL /bin/tcsh so if i make my script executable and run it output is ... (6 Replies)
Discussion started by: Deepak Dutt
6 Replies

6. UNIX for Dummies Questions & Answers

Bourne Shell Script that only takes command line arguments

Does anybody know how to Accept a “userid” as a command line argument on a Unix Bourne Shell Script? The output should be something like this: User userid has a home directory of /path/directory the default shell for this user is /path/shell (5 Replies)
Discussion started by: ajaira
5 Replies

7. UNIX for Advanced & Expert Users

Bourne Shell Script that only takes command line arguments

Does anybody know how to Accept a “userid” as a command line argument on a Unix Bourne Shell Script? The output should be something like this: User userid has a home directory of /path/directory the default shell for this user is /path/shell (1 Reply)
Discussion started by: ajaira
1 Replies

8. Shell Programming and Scripting

Bourne Shell Script that only takes command line arguments

Does anybody know how to Accept a “userid” as a command line argument on a Unix Bourne Shell Script? The output should be something like this: User userid has a home directory of /path/directory the default shell for this user is /path/shell (1 Reply)
Discussion started by: ajaira
1 Replies

9. Homework & Coursework Questions

I need to make a script that has 4 command line arguments to function properly.

I have no idea what the following means. The teacher is too advanced for me to understand fully. We literally went from running a few commands over the last few months to starting shell scripting. I am not a programmer, I am more hardware oriented. I wish I knew what this question was asking... (3 Replies)
Discussion started by: Wookard
3 Replies

10. Shell Programming and Scripting

KSH script: piping passes command-line arguments

Dear forum I have the following small script: #!/bin/ksh echo -e "abba-o" | awk -F '-' '{ print $2 }' | cut -b 1It needs to be ksh.. in bash I don't have this problem. If I run this on opensuse 10.2 I get this as output: e If I run this on suse enterprise 10 sp2 then I get this: o ... (1 Reply)
Discussion started by: gemtry
1 Replies
Login or Register to Ask a Question