Checking arguments


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Checking arguments
# 1  
Old 03-22-2011
Checking arguments

I shoe here the start of a csh script I have written. I am trying to write some code to check the arguments and if the arguments don't match the tags, I will abort and display an error.

For example using

Code:
./script.csh -r=10/20.30/40 -xyz=2/3/4

will give an error as the -xyz tag doea not exist.


Code:
  set ierr = 0
  set iarg = 0
  set opt_jbase = 0
  set opt_range = 0
  set opt_annot = 0
  set opt_fout = 0
  set opt_vrb = 0
  set opt_usage = 0
  set opt_examples = 0
  set opt_help = 0

  set fullnames_list = ""
  set fnames_list = ""
  set fext_list = ""
  set nf = 0

  set Version = "V04"
  alias MATH 'set \!:1 = `echo "\!:3-$" | bc -l`'

  set narg = $#argv
  while ($iarg < $narg)

    MATH iarg = $iarg + 1
    set arg = $argv[$iarg]
    set opt = `echo $arg | awk 'BEGIN {FS="="} {print $1}'`
    set par = `echo $arg | awk 'BEGIN {FS="="} {print $2}'`

    switch ($opt)
    case "-jb":
        set Ajbase = $par
        set bwidth = `echo $Ajbase | awk '{split($1,a,"/"); print a[1]}'`
        set bheight = `echo $Ajbase | awk '{split($1,a,"/"); print a[2]}'`
        set opt_jbase = 1
        breaksw
    case "-r":
        set Arange = $par
        set xmin = `echo $Arange | awk '{split($1,a,"/"); print a[1]}'`
        set xmax = `echo $Arange | awk '{split($1,a,"/"); print a[2]}'`
        set ymin = `echo $Arange | awk '{split($1,a,"/"); print a[3]}'`
        set ymax = `echo $Arange | awk '{split($1,a,"/"); print a[4]}'`
        set opt_range = 1
        breaksw
    case "-a":
        set Aannot = $par
        set adx = `echo $Aannot | awk '{split($1,a,"/"); print a[1]}'`
        set ady = `echo $Aannot | awk '{split($1,a,"/"); print a[2]}'`
        set afx = `echo $adx | awk '{print $1/2}'`
        set afy = `echo $ady | awk '{print $1/2}'`
        set ax = "a"$adx"f"$afx
        set ay = "a"$ady"f"$afy
        set opt_annot = 1
        breaksw
    case "-fout":
        set Afout = $par
        set fout = `echo $Afout | awk 'BEGIN { FS=".ps" } { print $1 }'`
        set opt_fout = 1
        breaksw
    case "-v":
        set opt_vrb = 1
        breaksw
    case "-usg":
        set opt_usage = 1
        breaksw
    case "-eg":
        set opt_examples = 1
        breaksw
    case "-h":
        set opt_help = 1
        breaksw
    default:
        set filename = `echo $arg | awk 'BEGIN {FS="."} {print $1}'`
        set filextension = `echo $arg | awk 'BEGIN {FS="."} {print $2}'`
        set fullnames_list = "$fullnames_list $arg"
        set fnames_list = "$fnames_list $filename"
        set fext_list = "$fext_list $filextension"
        MATH nf = $nf + 1
    endsw

  end   # while

# 2  
Old 03-22-2011
Move all command line arguments to appropriate variables, first, all, and then detect invalid combinations of variables.
# 3  
Old 03-22-2011
What you mean? Suppose a user tries an option that does not exist.
# 4  
Old 03-22-2011
Sorry. Couldn't find the question. What follows is just my guess at what you may want:
The processing under the 'default' tag in your switch looks like a pre-processing block that should be done before entering the switch. Your instructions inside the switch do something for each and every valid argument given, so your 'default' tag should be considered an 'otherwise' tag instead and include there whatever error processing you have in mind when the user inputs an illegal argument.
# 5  
Old 03-22-2011
Problem is you may have already done some processing before an invalid tag is discovered. If the script then bombs out with an invalid arguments error some work has already been done. This different to how every other unix command operates, and is quite dangerous.

As DGPickett implied, try to validate all the arguments first and then do the processing. Consider using getopt to simplify the argument parsing.

Last edited by Chubler_XL; 03-22-2011 at 07:08 PM..
# 6  
Old 03-22-2011
Yes, some things would have already been done, but it will only be about processing the information given (declaration of variables). So it does not matter if I stop and exit.

I use the dafault to pass filenames (no tags are used when passing certain files).

Can you give a small example that I can follow Chub?
# 7  
Old 03-22-2011
Yep, I didn't read you code closely enough. All the awk scripts are just pulling apart your arguments. Might be better to drop the = part and put parameter in next arg (this is the unix standard way) eg:

Code:
./script.csh -r 10/20.30/40 -xyz 2/3/4


This might get you started:

Code:
#!/bin/csh
set done=0
set opt_vrb
set ajbase
while ( $#argv != 0 && $done == 0 )
    switch ($1)
        case -jb:
            if ( $2 == "") then
                echo "option -jb requires argument"
                exit 2
            endif
            set ajbase=$2
            shift
            breaksw
        case --:
            set done=1
            breaksw
        case -v:
            set opt_vrb=1
            breaksw
        default:
            set F=`expr substr $1 1 1`
            if ( "$F" == "-" ) then
               echo "Invalid argument $1"
               exit 2
            endif
            set done=1
            breaksw
    endsw
    if ( $done == 0 ) then
        shift
    endif
end
if ( $opt_vrb == 1) then
   echo -v option set
endif
if ( "$ajbase" != "" ) then
   echo ajbase=$ajbase
endif
echo File Arguments left: $argv


Last edited by Chubler_XL; 03-22-2011 at 08:30 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Too many arguments

because it gives me this error if? while read linea do #echo "Archivos Entrada: $linea" largo=`awk '{print length($linea)}'` echo "largo : $largo " if ; then #Here's the problem, I take this line and it works echo "a es igual a 1" fi... (3 Replies)
Discussion started by: tricampeon81
3 Replies

2. Programming

Checking which arguments are supplied

I have written this C++ program and I am using getopt_long and want to chech when the user supplies the arguments so that I can put a default or otherwise. Currently I am using hasargv or Pc.get_string("key",s), Pc.get_real("key",s), etc to detect whether the user supplied a value. For... (3 Replies)
Discussion started by: kristinu
3 Replies

3. Programming

Checking which arguments are supplied

I have written this C++ program and I am using getopt_long (2 Replies)
Discussion started by: kristinu
2 Replies

4. Programming

Checking which arguments are supplied

I have written this C++ program and I am using getopt_long and (0 Replies)
Discussion started by: kristinu
0 Replies

5. SCO

Stop boot system at "Checking protected password and checking subsystem databases"

Hi, (i'm sorry for my english) I'm a problem on boot sco unix 5.0.5 open server. this stop at "Checking protected password and checking subsystem databases" (See this image ) I'm try this: 1) http://www.digipedia.pl/usenet/thread/50/37093/#post37094 2) SCO: SCO Unix - Server hangs... (9 Replies)
Discussion started by: buji
9 Replies

6. UNIX for Dummies Questions & Answers

How to take arguments?

Hey everybody. How do you write a program that will produce output based on its arguments? For example, how would you write one that will add 1 to an integer argument so it would look like this: $add 1 78 79 $ I only know how to write programs to take user input with the read function,... (2 Replies)
Discussion started by: unclepickle1
2 Replies

7. Shell Programming and Scripting

grep with two arguments to arguments to surch for

Hello, is it possible to give grep two documents to surche for? like grep "test" /home/one.txt AND /home/two.txt ? thanks (1 Reply)
Discussion started by: Cybertron
1 Replies

8. Homework & Coursework Questions

checking for number of arguments.

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Your script must check for the correct number of arguments (one argument). If somebody tries to invoke the... (1 Reply)
Discussion started by: brooksie91
1 Replies

9. Shell Programming and Scripting

Too many arguments

echo "the number from 1 to 10:" i=1 while do echo $i i=`expr $i+1' done above is the program i written in Linux O.S using vi editor but i am getting the error that while: line 3: i am not understanding that why i am getting this error. can any body please help me regarding this... (3 Replies)
Discussion started by: bsatishbabu
3 Replies

10. Shell Programming and Scripting

[: too many arguments

hi I am getting too many arguments error for the below line if ; then Thx in advance (1 Reply)
Discussion started by: vls1210
1 Replies
Login or Register to Ask a Question