How to use a case stmt in a for loop?


 
Thread Tools Search this Thread
Operating Systems Linux How to use a case stmt in a for loop?
# 1  
Old 07-01-2014
Wrench 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:

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

# 2  
Old 07-01-2014
What is the mapping between filenames in $svdumpdir and $npacdir? What if you have more than 7 files?
# 3  
Old 07-01-2014
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:

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

# 4  
Old 07-01-2014
Yes, you got it.
You certainly want | grep '\.gtt$'.
And you can augment your case statement with a last switch:
Code:
    *)  echo "$file: bad region $filer" ;;

This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 07-01-2014
Bear in mind that ls -rt $svdumpdir/* might return more than one file per line (unless your ls is aliased to something).
# 6  
Old 07-01-2014
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.

Last edited by MadeInGermany; 07-01-2014 at 05:08 PM..
This User Gave Thanks to MadeInGermany For This Post:
# 7  
Old 07-01-2014
Quote:
Shell aliases do not exist in bash/ksh scripts - only in interactive command line mode.
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...
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Case sensitive in If loop .

Hi All, select app from the menu: ABC DEF GHI JKL ALL # ALL will select all the apps in the menu echo "Enter your option" read option; if then <execute the below command> elif # option is the 1 selection from menu...not ALL <execute the below command> else (14 Replies)
Discussion started by: Devaraj A
14 Replies

2. Shell Programming and Scripting

Case loop condition

hello, I would like to do exit at the end ie list all errors before exiting How to put the token exit in a variable with a loop ? Thanks function g1 () { case "$1" in (-0-0 | -0-1 | -0-2 | -0-3 | -1-0 | -1-1 | -1-2 | -1-3) # nothing, OK ! ;; (*) echo 'Fatal, $1 = '"'$1'"', Date... (9 Replies)
Discussion started by: amazigh42
9 Replies

3. Shell Programming and Scripting

Case Stmt - Need Help Urgently

Hello All, Need help urgently.. i have a scenario where i have two files 1) mireport_20111406.txt 2) PRLIHSP01_8080.2011-06-11-15_26_31 ---------- I want a query something similar to this algorithm :- Case when file_name is like mireport then extract_date=14-06-2011 when... (4 Replies)
Discussion started by: iamnoone
4 Replies

4. Shell Programming and Scripting

Help With Loop in Case Statement script

I am writing a bash script that asks the user for input and I need it to repeat until the user selects quit.. I dont know how to write the loop for it I searched all over but i still do not get it.. if anyone could help with this it would be greatly apprciated here is my script so far: #!... (2 Replies)
Discussion started by: Emin_Em
2 Replies

5. Shell Programming and Scripting

Help with Dates and SQl stmt?

hi All Please explain the below statement in RED?What does this mean? perl /HDS/common/operations/Quality_Team/Nirvana/WEEKLY_OOPS/sql_dump.pl --sql "select * from WEEKLY_REPORT order by test_case, type" --username=ddb_qa --password=ddb_qa123 --sid=pldeldb --output... (1 Reply)
Discussion started by: SVS_2017
1 Replies

6. Shell Programming and Scripting

Help with awk stmt

Hi All, I have a log file as: cat error.log.tmp1 2010-07-06 23:18:34 Connection Available to xyztestftp.abc.com.my 2010-07-06 23:20:33 Connection Available to xyztestftp.abc.com.my ERROR FTP LOGIN Now I am reading this complete log file if no single occurrence of word "ERROR" (this word... (5 Replies)
Discussion started by: ss_ss
5 Replies

7. UNIX for Dummies Questions & Answers

Including case insensitivity into loop command

I have the following piece of code: for file in *csv; do cat $file >> $newfile; done for file in *CSV; do cat $file >> $newfile; done and would like to know how to combine these two rules into one? The combined rule would then include the case insensitivity that I am looking for. (4 Replies)
Discussion started by: figaro
4 Replies

8. UNIX for Dummies Questions & Answers

Please explain the stmt

hi, Please explain the below stmt. P=1234 var1=:/a/b/c/file.dat printf "%s %s\n" "$" "${var1#?}" Output is: 1234 /a/b/c/file.dat What is #? in printf stmt? by using that the first character( : ) of "var1" variable is not displayed in output. How is that? please explain.. ... (1 Reply)
Discussion started by: srilaxmi
1 Replies

9. Shell Programming and Scripting

case loop... repeat on bad input?

I'm trying to get a case statement to start over if an undefined option is selected... But I am ata loss on how to actually do it. Here is a quick example of what I have. Echo "1) do this/n 2) Do that/n 3) Quit/n Make a selection/n" Read answer Case answer in 1) Dothid;; 2) Dothat;;... (3 Replies)
Discussion started by: trey85stang
3 Replies
Login or Register to Ask a Question