Passing wildcards to cammand line arguments


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing wildcards to cammand line arguments
# 1  
Old 12-27-2010
Passing wildcards to cammand line arguments

I have a csh script plot-model.csh

I want to be able to pass wildcards for the input file names such as

Code:
./plot-model.csh *8x6smp.cmod

Currently I have to pass the full files names one after each other which I then store in a list. Then I proceed to plot the data within it.

Now I want to use wildcards, get all the files, store the file names in a list and continue as usual.

Code:
./plot-model.csh *8x6smp.cmod

Currrently when I pass the wildcard it is only taking the first file from the wildcard expansion.
# 2  
Old 12-27-2010
In your script, 1st store argument in a variable as:
Code:
arg="$@"

then use arg variable wherever needed, say echo "$arg".

If doesn't work then post your script pls, atleast the portion where argument passed to script is used.

Last edited by anurag.singh; 12-27-2010 at 05:25 PM..
# 3  
Old 12-27-2010
Well, wild cards are expanded by the shell, not by any other magic. Some commands take lists and iterate them:
Code:
int i ;
for ( i = 1 ; i < argc ; i++ ){
  if ( !freopen( argv[i], "r", stdin )){
    perror( argv[i] );
    continue ;
   }
  .
  .
  .
 }

# 4  
Old 12-27-2010
Situation is a bit more complicated. I also pass plotting option as arguments in addition to files. File names don't have a tag such as -r=

Below is part of the script

Code:
  set ierr = 0
  set iarg = 0
  set opt_tpath = 0
  set opt_base = 0
  set opt_image = 0
  set opt_path = 0
  set opt_rangexz = 0
  set opt_rangec = 0
  set opt_annot = 0
  set opt_contour = 0
  set opt_color = 0
  set opt_usage = 0
  set opt_example = 0
  set opt_help = 0

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

  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}'  \
                         | tr "[A-Z]" "[a-z]"`
    set par = `echo $arg | awk 'BEGIN {FS="="} {print $2}'`

    switch ($opt)
    case "-tpath":
        set Atpath = $par
        set tpath = $Atpath
        set opt_tpath = 1
        breaksw
    case "-jbase":
        set Abase = $par
        set basewidth  = `echo $Abase | awk '{split($1,a,"/"); print a[1]}'`
        set baseheight = `echo $Abase | awk '{split($1,a,"/"); print a[2]}'`
        set opt_base = 1
        breaksw
    case "-jimage":
        set Aimage = $par
        set imagewidth  = `echo $Aimage | awk '{split($1,a,"/"); print a[1]}'`
        set imageheight = `echo $Aimage | awk '{split($1,a,"/"); print a[2]}'`
        set opt_image = 1
        breaksw
    case "-r":
        set Arangexz = $par
        set xmin = `echo $Arangexz | awk '{split($1,a,"/"); print a[1]}'`
        set xmax = `echo $Arangexz | awk '{split($1,a,"/"); print a[2]}'`
        set zmin = `echo $Arangexz | awk '{split($1,a,"/"); print a[3]}'`
        set zmax = `echo $Arangexz | awk '{split($1,a,"/"); print a[4]}'`
        MATH zminp = -1.0 * $zmax
        MATH zmaxp = -1.0 * $zmin
        set zmin = $zminp
        set zmax = $zmaxp
        set opt_rangexz = 1
        breaksw
    case "-crange":
        set Arangec = $par
        set cmin = `echo $Arangec | awk '{split($1,a,"/"); print a[1]}'`
        set cmax = `echo $Arangec | awk '{split($1,a,"/"); print a[2]}'`
        set opt_rangec = 1
        breaksw
    case "-ann":
        set Aannot = $par
        set adx = `echo $Aannot | awk '{split($1,a,"/"); print a[1]}'`
        set adz = `echo $Aannot | awk '{split($1,a,"/"); print a[2]}'`
        set afx = `echo $adx | awk '{print $1/2}'`
        set afz = `echo $adz | awk '{print $1/2}'`
        set ax = "a${adx}f${afx}"
        set az = "a${adz}f${afz}"
        set opt_annot = 1
        breaksw
    case "-cont":
        set Acontour = $par
        set contour = $Acontour
        set ctc = `echo $Acontour | awk '{split($1,a,"/"); print a[1]}'`
        set cta = `echo $Acontour | awk '{split($1,a,"/"); print a[2]}'`
        set opt_contour = 1
        breaksw
    case "-color":
        set Acolor = $par
        set color = $Acolor
        set opt_color = 1
        breaksw
    case "-u":
        set Ausage = $par
        set opt_usage = 1
        breaksw
    case "-e":
        set Aexample = $par
        set opt_example = 1
        breaksw
    case "-h":
        set help = $par
        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 nxz = `echo $filename | awk 'BEGIN{FS="-"} {print $NF}'`
        set fullnames_list = "$fullnames_list $arg"  # Set full file names
        set fnames_list = "$fnames_list $filename"   # Set file names
        set fext_list = "$fext_list $filextension"   # Set file extensions
        MATH nf = $nf + 1
    endsw

  end   # while

  foreach f ($fnames_list)
    # process the file ...
  end

---------- Post updated at 04:38 PM ---------- Previous update was at 04:37 PM ----------

Typical call would be

Code:
../../../Scripts/plot-model.csh -r=0/80/0/12 -ann=10/2 -jimage=5/10 -crange=0.4/2.5 *8x6drw.cmod

# 5  
Old 12-27-2010
Well, it is usual to put them to the left and start from i > 1, just put your arg parse if's with a continue above the freopen, just like grep, cc, sort . . . .
# 6  
Old 12-27-2010
If you want to pass wildcards to a shell, just quote the expression containing them to avoid shell expansion. eg:
Code:
./plot-model.csh "*8x6smp.cmod"

# 7  
Old 12-27-2010
I think I've got it now. Sorry for the trouble.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Passing arguments from command line to switch case statement in C

Hi Am pretty new to C.. Am trying to pass the arguments from command line and use them in switch case statement.. i have tried the following #include <stdlib.h> main(int argc, char* argv) { int num=0; if ( argc == 2 ) num = argv; printf("%d is the num value",num); switch ( num ) ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

2. Shell Programming and Scripting

Reading a string and passing passing arguments to a while loop

I have an for loop that reads the following file cat param.cfg val1:env1:opt1 val2:env2:opt2 val3:env3:opt3 val4:env4:opt4 . . The for loop extracts the each line of the file so that at any one point, the value of i is val1:env1:opt1 etc... I would like to extract each... (19 Replies)
Discussion started by: goddevil
19 Replies

3. Shell Programming and Scripting

Passing arguments to python

How can I pass arguments to a python script??? (3 Replies)
Discussion started by: kristinu
3 Replies

4. Shell Programming and Scripting

Passing arguments to csh

I have noticed this thing using csh when passing arguments Suppose I call a csh script using ../Scripts/plot-model.csh -vmod="npt02-z30.vmod" -R="0/80/0/30" -c="0/4.5" -aspr="1:10" Somehow the " get removed when doing $argv ending up with -vmod=npt02-z30.vmod... (0 Replies)
Discussion started by: kristinu
0 Replies

5. UNIX for Dummies Questions & Answers

Passing arguments

I need to pass arguments to a shell script.My batch is calling some java program. ################# x=$1 y=$2 java -classpath program ################### if first parameter and second parameter is null then java -classpath program if first parameter is not null and second parameter is... (3 Replies)
Discussion started by: mnjx
3 Replies

6. Shell Programming and Scripting

Passing arguments to the subshell

I have a shell script which is invoked by passing an argument. The outer shell script calls another subshell and I want the argument passed down to flow down to the subshell. E.g Invoking a shell ======>> abc_refresh.ksh NM Below is the content of abc_refresh.ksh Value1=$1... (7 Replies)
Discussion started by: Mihirjani
7 Replies

7. Shell Programming and Scripting

passing arguments

Hi I have a script to which I pass multiple arguments, for example lets say the script name is "abc". I run the script like ./abc def /file <directory location> In the above "def" is the first argument and "/file" is the second argument. I expect <directory location> that is passed after... (4 Replies)
Discussion started by: zmfcat1
4 Replies

8. Shell Programming and Scripting

Passing Arguments-Help

Hi, I have a script which adds the user credentials to an ldap server. Im passing the variables as below.. /path/my_script $uname $pwd $environ ${deposit} If i enter some special characters like ';' in $pwd, script returns an error which is set to display if the user enters... (5 Replies)
Discussion started by: Tuxidow
5 Replies

9. UNIX for Dummies Questions & Answers

Passing arguments to an alias

I want to have an alias for the command fold -78 filename | lp How do I set my alias so that the argument passed is filename ?? alias lp='fold -78 | lp' then lp filename wont work cuase this is fold -78 | lp filename (1 Reply)
Discussion started by: pmcg
1 Replies

10. UNIX for Dummies Questions & Answers

passing arguments

I'm trying to pass a filename, or all the files in the current directory to the ls command with a script. Unsuccessful so far, here are a few of my attempts: #!/bin/ksh read fname #if (( $# > 0 )); then $fname | ls -l #fi this produces a long listing of all the files in my current... (4 Replies)
Discussion started by: jpprial
4 Replies
Login or Register to Ask a Question