How can multiple arguments be passed to shell script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can multiple arguments be passed to shell script?
# 1  
Old 04-25-2014
How can multiple arguments be passed to shell script?

My requirement is that I want to pass similar argument to a shell script and process it in the script. Something like below:

Code:
myScript.sh   -c COMPONENT1 -c COMPONENT2  -a APP

Note: -c option can be specified multiple times and -a is optional parameter

I know this can be achieved using parser.add_option in Perl. But can be this be done in Shell?
# 2  
Old 04-25-2014
getopts might help you. Have a look at this sample script:
Code:
m@home$cat test.sh
#! /bin/bash

#An array to hold the values passed using -c option
declare -a c_arr

#The count of -c options
c_count=0

#Go through all the optins passed
while getopts ":c:a:" o
do
        case "${o}" in
        c)
        c_arr[$c_count]=${OPTARG}
        let c_count=$c_count+1
        ;;

        a)
        a=${OPTARG}
        ;;

        *)
        echo "Wrong usage"
        exit 1
        ;;
        esac
done

if [ $c_count -eq 0 ]
then
        echo "option -c is mandatory"
        exit 1
fi

let i=$c_count-1

while [ $i -ge 0 ]
do
        echo "Got value for c option: ${c_arr[$i]}"
        let i=$i-1
done

if [ ! -z $a ]
then
        echo "Got value for a option: $a"
fi

And the output
Code:
m@home$./test.sh -c C1 -c C2 -a A
Got value for c option: C2
Got value for c option: C1
Got value for a option: A

This User Gave Thanks to chacko193 For This Post:
# 3  
Old 04-25-2014
Thanks a lot chacko193 !!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to call Oracle function with multiple arguments from shell script?

Dear All, I want to know how can i call oracle function from shell script code . My oracle function have around 5 input parameters and one return value. for name in *.csv; do echo "connecting to DB and start processing '$name' file at " echo "csv file name=$x" sqlplus -s scoot/tiger <!... (2 Replies)
Discussion started by: Balraj
2 Replies

2. Shell Programming and Scripting

Passing multiple arguments to a shell script

Hi Gurus, Need some help with the shell scripting here. #!/bin/ksh ps -ef | grep -i sample.ksh | grep -v grep > abc.txt if then echo "sample.ksh is executing" else echo "sample.ksh is not executing" fi (1 Reply)
Discussion started by: jayadanabalan
1 Replies

3. Shell Programming and Scripting

Deleting columns passed as arguments to the script

Hi all, I am trying to delete columns in a file using a script. The columns that need to be deleted are passed as arguments to the script. The script should look like this > delete_columns.sh <file_name.txt> <column_numbers_to_be_deleted> The contents of the file_name.txt will be like ... (5 Replies)
Discussion started by: VNR
5 Replies

4. UNIX for Advanced & Expert Users

Function not called when no arguments is passed

Hi Guys, I am trying to pass arguments to the script i am wrinting. When no argument is passed or wrong argument is passed, the script needs to output the way it needs to be called and exit. Currently, when no arguments is passed, it is not getting exited but goes on assuming those... (3 Replies)
Discussion started by: mac4rfree
3 Replies

5. Shell Programming and Scripting

Help required in passing multiple arguments from a shell script to a pl/sql block

Hi, hope everyone are fine. Please find my issue below, and I request your help in the same In a configuration file, i have a variable defined as below TEST = 'One','Two','Three' I am trying to pass this variable in to a sql script which is define in a pl/sql block as follows, In the... (1 Reply)
Discussion started by: ramakanth_burra
1 Replies

6. Red Hat

how can i know what arguments are passed to a pre install rpm script?

hi, i have an rpm, and i am looking at the presinstall script. i can see it takes in an argument, but what i do not know is how this argument is passed to the script? is there something that calls the preinstall script? i thought the preinstall script was the first thing executed. thanks (2 Replies)
Discussion started by: JamesByars
2 Replies

7. Shell Programming and Scripting

Getting the file name passed to a shell script

Hi all I want to use the filename passed into a shell script within the shell it self. The file will be passed as shown below ./myScript < myFile.file i tried $1 but i think since myFile is not passed as a command line arg its not saving the file name in $1 any help is appreciated. (5 Replies)
Discussion started by: sridanu
5 Replies

8. Shell Programming and Scripting

Check if passed arguments is users

i want to check passed arguments one by one and if it is user print home director of that user (3 Replies)
Discussion started by: testman84
3 Replies

9. Shell Programming and Scripting

How to pass arguments to SQL file passed in shell script?

Hi, I am using SYBASE database. in my script i am connecting to DB via using isql. isql -U${S_USER} -S${S_SERV} -D${S_DB} -P${S_PWD} -b0 -w3000 -h0 -s"|" -i${MYDIR}/ABC.sql -oXYZ.txt << FINSQL i am taking a ABC.sql file to use the queries written in it and storing the output in... (3 Replies)
Discussion started by: dazdseg
3 Replies

10. Shell Programming and Scripting

count no of arguments passed to a function

hi i have a function abc { //from this function i am passing args to antoher function like def a b c j k l } now i want to count the no of args coming to def() function and iterate over those values is there any way to do this one please help (2 Replies)
Discussion started by: satish@123
2 Replies
Login or Register to Ask a Question