Processing Multiple Arguments in Command Line Options


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Processing Multiple Arguments in Command Line Options
# 1  
Old 07-05-2013
Processing Multiple Arguments in Command Line Options

Hi All,
I am new to scripting. Could you please assist me .

Here is my requirement. I have written a script that has 2 option flags defined.

-l) calls some function with the arguments passed in front of -l
-r) calls second function with the arguments passed in front of -r
*) calls the third function with arguments specified.

For this I have written-
Code:
#!/usr/bin/bash
 
while [ $# -gt 0 ]
do
 
case "$1" in

-l) echo "$2";
    list_files "$2"
    shift;;
-r)usage;;
*)if [ "$1" == "." ];then 
 echo "Not allowed "
 else
 check_path "$1" 
 fi 
 shift ;;
esac
done
######################################

While executing-

Scenario 1- ./test -l file1 file2 file3
I am only able to capture the argument file1 with -l . The arguments file2 and file3 are passed to the *) option.

Could you please let me know how to pass all the arguments to the function called from -l) option.

Thanks a lot

Last edited by vbe; 07-05-2013 at 08:58 AM.. Reason: code tags
# 2  
Old 07-05-2013
Code:
-l) echo "$2";
    list_files "$2"
    shift;;

Since you use $2 here, should you not shift 2 for a start?
...
# 3  
Old 07-05-2013
I tried shift 2 but it wouldn't work. -

Since i need to call a function check_path if no option (-l etc) is passed, it is becoming increasingly difficult for me to capture multiple arguments passed to an option. Smilie .
# 4  
Old 07-05-2013
what about a loop using for:
Code:
for i in $*
do
  case $i in
...

# 5  
Old 07-05-2013
Enclose the arguments for -l with double quotes and parse them one by one like vbe stated with a for loop for example.

Code:
$ ./test -l "file1 file2 file3"

# 6  
Old 07-05-2013
@zaxxon - It worked partially. I was able to list the arguments passed to -l option.
The only problem was that, these arguments were also passed to the *) option.
# 7  
Old 07-05-2013
* would normaly be used for other "unwanted" cases, so you are to define precisely what you expect as argument for your third case...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple search options in find command

Hi, I'd like find multiple file options to fetch different types of files. find /path...// -type f -name "*.log" -o -name "*.req" -o -name "*.txt" -mtime +5 -exec ls -l {} \; Where as in the above command only the last .txt files its retriving but not .log and .req files can body help... (1 Reply)
Discussion started by: Y.balakrishna
1 Replies

2. Shell Programming and Scripting

Command line arguments with multiple values

how can I pass multiple values from command line arguments example script.sh -arg1 op1 -arg2 op1 op2 op3 (2 Replies)
Discussion started by: nsk
2 Replies

3. UNIX for Dummies Questions & Answers

UNIX find command - using multiple -name options

Hi all, What am trying do is search for a directory that is owned by cm that only exists in a path that has a particular directory ex: what I'm using now is find . -user cm -name '*.rel' -type d -exec ls -dl {} \; This is giving me stuff like this ./../../foo1/../../*.rel... (2 Replies)
Discussion started by: jtmed
2 Replies

4. Shell Programming and Scripting

[Solved] Sed error - multiple number options to `s' command

Hi All, I am having two files (file1 & file2) and a filelist.txt file below. file1: $$STRINGVAR1=5 $$STRINGVAR2=10 $$LAST_UPD_DT_TBL1=12/12/2010 12:00:00 $$STRINGVAR3=100 $$LAST_UPD_DT_TBL2=01/01/2010 12:00:00... (8 Replies)
Discussion started by: Chandru_Raj
8 Replies

5. Shell Programming and Scripting

Shell script to read multiple options from file, line by line

Hi all I have spent half a day trying to create a shell script which reads a configuration file on a line by line basis. The idea of the file is that each will contain server information, such as IP address and various port numbers. The line could also be blank (The file is user created). Here... (1 Reply)
Discussion started by: haggismn
1 Replies

6. UNIX for Dummies Questions & Answers

Running set options from the command line and bash command

I'm reading about debugging aids in bash and have come across the set command. It says in my little book that an addition to typing set you can also use them "on the command line when running a script..." and it lists this in a small table: set -o option Command Line... (5 Replies)
Discussion started by: Straitsfan
5 Replies

7. Shell Programming and Scripting

find command to use multiple -exec options

Hello All, Is there a way to make exec do a couple of operations on a single input from find? For example, find . -type d -exec ls -l "{}" ";" I would like to give the result of each "ls -l" in the above to a wc. Is that possible? I want to ls -l | wc -l inside exec. How do I... (1 Reply)
Discussion started by: prasanna1157
1 Replies

8. Shell Programming and Scripting

Using perl to get options from command line

Hi all, I want to get options from command line by perl. usage() options: -h Show this help message and exit -t Name of tester --timeout Set the timeout -l ... (1 Reply)
Discussion started by: Damon_Qu
1 Replies

9. Programming

Combining multiple command line arguments

suppose the user enters: ./Myfile blah1 blah2 blah3 I want to be able to read that in as: blah1blah2blah3 IN ONE VARIABLE Obviously I can print it out in one line excluding the white space, but I'm having trouble combining argv, argv, ....., argv together! This is what I tried, but I... (3 Replies)
Discussion started by: hansel13
3 Replies

10. Shell Programming and Scripting

how to? launch command with string of command line options

my description from another thread... here's my code: #!/bin/bash IFS=$'\n' function OutputName() { input=$1 echo $input input=`echo "$input" | sed -e 's/.//'` input=`echo "$input".avi` output_name=$input } if ]; then echo... (5 Replies)
Discussion started by: TinCanFury
5 Replies
Login or Register to Ask a Question