How to use a case stmt in a for loop?

How can I merge the move statements with the "FOR" loop to do a move of a file right after it zips it and not wait until all of the files are zipped to move all outisde the for loop.

Here is my current code:

for file in `ls -rt $svdumpdir/* | grep -v '.gz$' | grep '.gtt$' `
do
  echo "gzip $file"
  gzip -9 $file
done
endsec=`perl -e 'print STDOUT time();' `
runsecs=$(($endsec-$startsec))
timeval=`date`
echo "$timeval:zipped all files within $runsecs seconds"
mv $svdumpdir/lsms4iss_reg1.gtt.gz $npacdir/MW/$yyyymmdd.gtt.gz
mv $svdumpdir/lsms4iss_reg2.gtt.gz $npacdir/MA/$yyyymmdd.gtt.gz
mv $svdumpdir/lsms4iss_reg3.gtt.gz $npacdir/NE/$yyyymmdd.gtt.gz
mv $svdumpdir/lsms4iss_reg4.gtt.gz $npacdir/SE/$yyyymmdd.gtt.gz
mv $svdumpdir/lsms4iss_reg5.gtt.gz $npacdir/SW/$yyyymmdd.gtt.gz
mv $svdumpdir/lsms4iss_reg6.gtt.gz $npacdir/WE/$yyyymmdd.gtt.gz
mv $svdumpdir/lsms4iss_reg7.gtt.gz $npacdir/WC/$yyyymmdd.gtt.gz

What is the mapping between filenames in $svdumpdir and $npacdir? What if you have more than 7 files?

Only the 7 files will be there.

---------- Post updated at 10:52 AM ---------- Previous update was at 09:44 AM ----------

I believe i figured it out and it is working as expected. Here is my revised code:

for file in `ls -rt $svdumpdir/* | grep -v '.gz$' | grep '.gtt$' `
do
  echo "gzip $file"
  gzip -9 $file
  filer=`echo $file  | cut -c36-39`
  case $filer in
    'reg1') echo "Zipped and moved region 1 to $npacdir/MW"
            mv $svdumpdir/lsms4iss_reg1.gtt.gz $npacdir/MW/test-$yyyymmdd.gtt.gz ;;
    'reg2') echo "Zipped and moved region 2 to $npacdir/MA"
            mv $svdumpdir/lsms4iss_reg2.gtt.gz $npacdir/MA/test-$yyyymmdd.gtt.gz ;;
    'reg3') echo "Zipped and moved region 3 to $npacdir/NE"
            mv $svdumpdir/lsms4iss_reg3.gtt.gz $npacdir/NE/test-$yyyymmdd.gtt.gz ;;
    'reg4') echo "Zipped and moved region 4 to $npacdir/SE"
            mv $svdumpdir/lsms4iss_reg4.gtt.gz $npacdir/SE/test-$yyyymmdd.gtt.gz ;;
    'reg5') echo "Zipped and moved region 5 to $npacdir/SW"
            mv $svdumpdir/lsms4iss_reg5.gtt.gz $npacdir/SW/test-$yyyymmdd.gtt.gz ;;
    'reg6') echo "Zipped and moved region 6 to $npacdir/WE"
            mv $svdumpdir/lsms4iss_reg6.gtt.gz $npacdir/WE/test-$yyyymmdd.gtt.gz ;;
    'reg7') echo "Zipped and moved region 7 to $npacdir/WC"
            mv $svdumpdir/lsms4iss_reg7.gtt.gz $npacdir/WC/test-$yyyymmdd.gtt.gz ;;
  esac
done

Yes, you got it.
You certainly want | grep '\.gtt$' .
And you can augment your case statement with a last switch:

    *)  echo "$file: bad region $filer" ;;
1 Like

Bear in mind that ls -rt $svdumpdir/* might return more than one file per line (unless your ls is aliased to something).

You mean the multi-column output if ls prints to a terminal?
In this case (and most cases) the destination is not a terminal.
In interactive scripts one could add a -1 option: ls -1rt
--
Shell aliases do not exist in bash/ksh scripts - only in interactive command line mode.

1 Like

Hrm, I remember an issue with an RHEL script which was running really slowly because ls was aliased to use colour (which stats every file, tens of thousands in this case).

iirc, it wasn't being sourced (which would use an alias), but some quick testing on cygwin indicates that executing with or without a hashbang definately does not, as you say...

Note that ls never puts more than one filename on a line if the output is not connected to a terminal device. The -1 option is always effectively turned on when ls output is sent through a pipe.

1 Like

Just guessing: If using e.g. bash, and if your filenames are built consistently, with the region number in the 13th char, you could move the files without the case statement:

DEST=(MW MA NW SE SW WE WC)
for ...
mv $svdumpdir/${file} $npacdir/${DEST[${file:12:1}]}/$yyyymmdd.gtt.gz 
... done