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

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

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

Currrently when I pass the wildcard it is only taking the first file from the wildcard expansion.

In your script, 1st store argument in a variable as:

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.

Well, wild cards are expanded by the shell, not by any other magic. Some commands take lists and iterate them:

int i ;
for ( i = 1 ; i < argc ; i++ ){
  if ( !freopen( argv, "r", stdin )){
    perror( argv );
    continue ;
   }
  .
  .
  .
 }

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

  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

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

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

If you want to pass wildcards to a shell, just quote the expression containing them to avoid shell expansion. eg:

./plot-model.csh "*8x6smp.cmod"

I think I've got it now. Sorry for the trouble.

I suppose there is a nice C lib to expand them for you, later (popen( "ls wildcards", "r" )) does not count!)? I always let the outer shell do it, or a wrapper script!