passing an option as an argument!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting passing an option as an argument!
# 1  
Old 05-03-2012
passing an option as an argument!

Hi Folks
I have got to the point where I can specify the arguments but how to pass an option is still mystery to me. Example:
Code:
temp.csh a b c d
 
set temp1 = $argv[1]
set temp2 = $argv[2]
set temp3 = $argv[3-]
echo $temp1
a
echo $temp2
b
echo $temp3
c d
I WANT:
temp.csh a b c d -S 1
set temp1 = $argv[1]
set temp2 = $argv[2]
set temp3 = $argv[3-]
#Checks in argument if -S 1 is passed or not first and than
set skip = 1
#########
echo $temp1
a
echo $temp2
b
echo $temp3
c d
echo $skip
1

I know I can use switch and Case statement but I have done it when I am not expecting an array in temp 3 but only 1 element. Please Help. N.B: $temp3 can have more than 2 arguments.
Thanks in Advance
# 2  
Old 05-03-2012
How about this:

Code:
#!/bin/csh
set argv=`getopt abcdS: $*`
while(1)
    switch ($1:q)
        case -a:
                echo "a flag" ; shift
                breaksw
        case -b: 
                echo "b flag" ; shift
                breaksw
        case -c: 
                echo "c flag" ; shift
                breaksw
        case -d 
                echo "d flag" ; shift
                breaksw
        case -S: 
                echo "option S, argument $2:q" ; shift ; shift
                breaksw
        case --:
                shift
                break
        default:
                echo "Usage: $0 [-a] [-b] [-c] [-d] [-S n]"
                exit 1
    endsw
end

# 3  
Old 05-04-2012
Thanks a Ton! It works perfectly !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Passing a second argument

I am trying to pass a second argument like so: if ] then export ARG2=$2 else message "Second argument not specified: USAGE - $PROGRAM_NAME ARG1 ARG2" checkerror -e 2 -m "Please specify if it is a history or weekly (H or W) extract in the 2nd argument" fi however, it always goes... (4 Replies)
Discussion started by: MIA651
4 Replies

2. UNIX for Beginners Questions & Answers

Missing argument for option: n Error

I am trying to execute the cli.sh script in another shell script passing arguments and getting the below error. Myscript.sh #!/bin/sh /home/runAJobCli/cli.sh runAJobCli -n $Taskname -t $Tasktype I am passing the below 2 arguments and it giving error ./Myscript.sh T_SAMPLE_TEST MTT... (11 Replies)
Discussion started by: Info_Geek
11 Replies

3. Shell Programming and Scripting

ksh - default value for getopts option's argument

Hello everyone, I need help in understanding the default value for getopts option's argument in ksh. I've written a short test script: #!/bin/ksh usage(){ printf "Usage: -v and -m are mandatory\n\n" } while getopts ":v#m:" opt; do case $opt in v) version="$OPTARG";; ... (1 Reply)
Discussion started by: da1
1 Replies

4. Shell Programming and Scripting

Passing argument not retrieving.

Hi I have a script which am trying to pass an argument which am trying to call using $1 but its not taking the value inside the if loop as it showing the error as if: Empty if.. Any help on this will be helpful. #!/usr/bin/csh echo $1 if ('$1' == "pp") then echo "Printing $1" endif (4 Replies)
Discussion started by: rogerben
4 Replies

5. Shell Programming and Scripting

Argument passing

How to pass the alphabet character as a argument in case and in if block? ex: c=$1 if a-z ]] then echo "alphabet" case $1 in a-z) echo "the value is a alphabet" edit by bakunin: please use CODE-tags. We REALLY mean it. (9 Replies)
Discussion started by: Roozo
9 Replies

6. Shell Programming and Scripting

Help with passing argument

Hi, I have a script that is scheduled with cron and runs every night. The cron part looks like this: 00 20 * * 0,1,2,3,4,5,6 /usr/local/bin/BACKUP TBTARM HOT DELETE My issue is with the 3rd parameter. Somewhere in the script, i want to tell the script to delete some files if the 3rd... (7 Replies)
Discussion started by: dollypee
7 Replies

7. Shell Programming and Scripting

Help with Passing argument and testing

Hi all First of all thanks for everyone to read by doubt.Am beginner in shell scripting Following are my doubts i have to pass an argument to shellscript how can i do that second i have to test the argument and shows error when nothing is passes third i have to match exact argument... (3 Replies)
Discussion started by: zeebala1981
3 Replies

8. Shell Programming and Scripting

passing Argument

Hi All, i have script like below.. echo "1) first option" echo "" echo "2) second option" echo "" echo "*) please enter the correct option" read select case $select in 1) echo "first option selected" ;; 2) echo "second option selected" ;; *) echo "please enter the correct... (4 Replies)
Discussion started by: Shahul
4 Replies

9. Shell Programming and Scripting

option followed by : taking next option if argument missing with getopts

Hi all, I am parsing command line options using getopts. The problem is that mandatory argument options following ":" is taking next option as argument if it is not followed by any argument. Below is the script: while getopts :hd:t:s:l:p:f: opt do case "$opt" in -h|-\?)... (2 Replies)
Discussion started by: gurukottur
2 Replies

10. Solaris

ps: 65535 is an invalid non-numeric argument for -p option

I want to figure out what is the reason of error message I have in Solaris 10. Why Solaris 10 dosn't recognize 65535? ps: 65535 is an invalid non-numeric argument for -p option usage: ps 'format' is one or more of: Thank you (5 Replies)
Discussion started by: gogogo
5 Replies
Login or Register to Ask a Question