Copying multiple files with "If Then Else" logic


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Copying multiple files with "If Then Else" logic
# 1  
Old 06-11-2008
Copying multiple files with "If Then Else" logic

I need some suggestions on how to write the code to copy multiple files rather than duplicating the code multiple times.

Example: I have four files that need to go throught this logic in the same way. Do I have to duplicate this chunk of code four times or can I built the logic in the same set of code without copying and pasting the code four times with different file names?

Please advice...

This is what the code does for one file....

1) copies the file from one directory to another
2) Strips when the date part from the ll command and writes to the log
3) Counts the number of records in the file and writes to the log
4) If record count is less than 1, it exits with an error code

In step 3, I am not sure if cat is the most effective way to do a wc -l.

Code:
cp -f $FTP/fwsalrpt.textp $EXPORT

RC=$?

if [ $RC -eq 0 ]
then
   DAY=$(ll $FTP | awk '/fwsalrpt.textp/ {print $6 $7 substr($8,1,5)}' )
   echo "*** ftp file from $DAY "  >>  $LOG
   RECORDS=$(cat $FTP/fwsalrpt.textp |wc -l )
   echo '*** Number of ftp fwsalrpt.textp records = ' $RECORDS >> $LOG
   if [ $RECORDS -lt 1 ]
   then
      echo "*** No data in input file "  >>  $LOG
      echo "*** fwcsales.rplftp.job failed "  >> $LOG
      date >> $LOG
      exit 3
   fi
else
   echo "*** copy of IBM file aborted"  >> $LOG
   echo "*** exit = " $RC  >> $LOG
   echo "*** fwcsales.rplftp.job failed "  >> $LOG
   date >> $LOG
   exit 2
fi

# 2  
Old 06-12-2008
Hammer & Screwdriver Why not hold four filenames in a separate file?

Make file_list contain:
file_to_do_1
file_to_do_2
file_to_do_3
file_to_do_4

Then, your program just needs an outer loop:

Code:
while read zf
  do
#put read filename into your program variable
  file_to_process="$zf"

  (program loop - your existing code)
done <file_list

# 3  
Old 06-12-2008
Thanks Joey...

I did create a filename with the list of files that need to be copied...But I don't see much of a change in code because I am getting a count of records for each file and also using AWK to print the date part and then writing it to the log...

I am just trying to find an efficient way to write the code.

Code:
while read FILENAME
do
  cp -f ${FTP}/$FILENAME ${EXPORT}/${FILENAME}
  if [ $? -eq 0 ] 
  then
   DAY=$(ll $FTP | awk '/fwsalrpt.textp/ {print $6 $7 substr($8,1,5)}' )
   echo "*** FTP file from $DAY "  >>  $LOG
   RECORDS=$(cat $FTP/fwsalrpt.textp |wc -l )
   echo '*** Number of ftp fwsalrpt.textp records = ' $RECORDS >> $LOG
   if [ $RECORDS -lt 1 ]
   then
      echo "*** No data in input file "  >>  $LOG
      echo "*** nz.fwcsales.rplftp.job failed "  >> $LOG
      date >> $LOG
      exit 3
   fi
else
   echo "*** Copy of IBM file aborted"  >> $LOG
   echo "*** exit = " $RC  >> $LOG
   echo "*** nz.fwcsales.rplftp.job failed "  >> $LOG
   date >> $LOG
   exit 2
fi 

  cp -f ${FTP}/$FILENAME ${EXPORT}/${FILENAME}
  if [ $? -eq 0 ] 
  then
   DAY=$(ll $FTP | awk '/fwmslrpt.textp/ {print $6 $7 substr($8,1,5)}' )
   echo "*** FTP file from $DAY "  >>  $LOG
   RECORDS=$(cat $FTP/fwmsalrpt.textp |wc -l )
   echo '*** Number of ftp fwmsalrpt.textp records = ' $RECORDS >> $LOG
   if [ $RECORDS -lt 1 ]
   then
      echo "*** No data in input file "  >>  $LOG
      echo "*** nz.fwcsales.rplftp.job failed "  >> $LOG
      date >> $LOG
      exit 3
   fi
else
   echo "*** Copy of IBM file aborted"  >> $LOG
   echo "*** exit = " $RC  >> $LOG
   echo "*** nz.fwcsales.rplftp.job failed "  >> $LOG
   date >> $LOG
   exit 2
fi
done < {FTP}/fwcsales_filenames.txt

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml"

Dear Ladies & Gents, I have a requirement to delete all the log files in /var/log/test directory that are older than 10 days and their first line begin with "MSH" or "<?xml" or "FHS". I've put together the following BASH script, but it's erroring out: for filename in $(find /var/log/test... (2 Replies)
Discussion started by: Hiroshi
2 Replies

2. UNIX for Advanced & Expert Users

"GET" command retrieves multiple files while using wildcard

Hi All I am using GNU/Linux This is regarding the get command to retrieve files (filename with wild card characters) from remote server. I thought Get command can retrieve only 1 file irrespective of the files it has on the remote server And it is the function of mget to retrieve all... (7 Replies)
Discussion started by: sparks
7 Replies

3. Shell Programming and Scripting

Find out if multiple files have lines ending with"r"

I am trying to find out which files in a group of files have lines ending in r. What I have is this: cat /tmp/*RECORDS| if grep r$>/dev/null; then echo "yes";else echo"no";fi Records is more than one file. There are the following files TEST-RECORDS /volume/testing /volume/programs ... (2 Replies)
Discussion started by: newbie2010
2 Replies

4. UNIX for Advanced & Expert Users

How to gzip files "on fly" before copying

Hello, I want to gzip some files before copying to remote host. There is no freespace on source host so it needs to be perfomed within one-liner. I tried the following but it didn't work gzip -c -9 all_rvds.xml |ssh targethost "dd of=/tmp/all_rvds.xml.gz" cat all_rvds.xml |gzip -c9 |ssh... (5 Replies)
Discussion started by: urello
5 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

"Join" or "Merge" more than 2 files into single output based on common key (column)

Hi All, I have working (Perl) code to combine 2 input files into a single output file using the join function that works to a point, but has the following limitations: 1. I am restrained to 2 input files only. 2. Only the "matched" fields are written out to the "matched" output file and... (1 Reply)
Discussion started by: Katabatic
1 Replies

7. Shell Programming and Scripting

Compiling multiple ".c" files starting with xxx

Hello, I am trying to figure out how I can write a bashscript that compiles several ".c" files that start with xxx (example: xxx_try.c and xxx_that.c) So I want to compile all these files with a bash script. Anyone can help pls? (6 Replies)
Discussion started by: Freak79
6 Replies

8. UNIX for Advanced & Expert Users

Why is wget copying my directory tree with some files with "@"?

I'm using wget 1.11.4 on Cygwin 1.5.25. I'm trying to recursively download a directory tree, which is the root of a javadoc tree. This is approximately the command line I tried: wget -x -p -r http://<host>/.../apidoc When it finished, it seemed like it downloaded... (0 Replies)
Discussion started by: dkarr
0 Replies

9. Shell Programming and Scripting

Awk - to test multiple files "read" permission ?

Hi Masters, Iam new to this Forum and this is my first post. My question is: I've some datafiles belongs the type (A, B, C) in the location 'export/home/lokiman ' dataA1.txt dataB28.txt dataC35.txt 1) I've to check the read permission for each file, if it not there then I've to... (1 Reply)
Discussion started by: lokiman
1 Replies

10. UNIX for Dummies Questions & Answers

Copying all files of type "pdf"

Hi, Within a shell script I'm trying to copy all the files in a directory structure to another folder but only the files of type "pdf" I have made a start but I can't work out how to finish it. find $ATEBUILD/doc/user -name "*.pdf" | xargs cp THEN WHAT? I hope this makes sense! Rob (6 Replies)
Discussion started by: milage
6 Replies
Login or Register to Ask a Question