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-

#!/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 [b]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

-l) echo "$2";
    list_files "$2"
    shift;;

Since you use $2 here, should you not shift 2 for a start?
...

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. :frowning: .

what about a loop using for:

for i in $*
do
  case $i in
...

Enclose the arguments for -l with double quotes and parse them one by one like vbe stated with a for loop for example.

$ ./test -l "file1 file2 file3"

@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.

  • would normaly be used for other "unwanted" cases, so you are to define precisely what you expect as argument for your third case...

You might also check if getopts is helpful for what you want to achieve.