Looping on a list of files...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Looping on a list of files...
# 15  
Old 04-05-2007
I have a directory that I would like to check for files of a certain pattern, that pattern being "gspp*". There may be several files or there may be no files that fit the given pattern at the time the script is run. In the event a file(s) is found, I would like to rename it to a temporary file, load this file into a database using sql loader and then move it into another directory when I'm done.

The process works perfectly when there is one or more files that match the pattern. When there are none, I run into a problem. When there are none, it still tries to do all of the other steps (renaming, loading, moving) on a filename that does not exist. I do not want this to occur.

Thanks.
# 16  
Old 04-05-2007
Quote:
Originally Posted by lazerfoursix
I have a directory that I would like to check for files of a certain pattern, that pattern being "gspp*". There may be several files or there may be no files that fit the given pattern at the time the script is run. In the event a file(s) is found, I would like to rename it to a temporary file, load this file into a database using sql loader and then move it into another directory when I'm done.

The process works perfectly when there is one or more files that match the pattern. When there are none, I run into a problem. When there are none, it still tries to do all of the other steps (renaming, loading, moving) on a filename that does not exist. I do not want this to occur.

Thanks.
OK, that's much clearER.
Couple of notes:
  1. you don't need a 'grep' in the 'for' loop. You already wildcarded you file names and the shell will expand the file names ok. This is what reborg has suggested in the earlier post.
  2. you seems to be missing a a ${FILE} in your 'sqlldr' command. It looks like you're moving it PRIOR to sqlldr, never referencing it in sqlldr AND moving it again somewhere else after the sqlldr. All of that without actually loading it. What's the point of doing so many 'mv'-s?
# 17  
Old 04-05-2007
The sqlldr command is fine. Yes, I am moving it prior to calling sqlldr. I am renaming it from a filename that varies day-to-day to a static temporary one. Then my control file references that static temporary one in its body. After it is loaded, I rename the file to its old name and then archive it.

I'll remove that, but I still ask, how do I ensure it's not hitting the else block? Do I check if $FILES = null?
# 18  
Old 04-05-2007
You don't need the if statement at all.

Code:
for FILES in gspp*
do
    echo "Loading $FILES" >> ../path/mylog.log
    mv $FILES temp.tmp
    sqlldr user/pass@DB control=/path/control.ctl
    bad=/path/bad.bad log=/path/log.log
    mv temp.tmp ../archive/$FILES
    echo "Load of $FILES complete!" >> ../path/mylog.log
    echo "Deleting remote $FILES" >> /path/mylog.log
    /path/delfile.sh $FILES
    ftp -nvi < delfile.ftp >> ../path/mylog.log
done

# 19  
Old 04-05-2007
Quote:
Originally Posted by reborg
You don't need the if statement at all.

Code:
for FILES in gspp*
do
    echo "Loading $FILES" >> ../path/mylog.log
    mv $FILES temp.tmp
    sqlldr user/pass@DB control=/path/control.ctl
    bad=/path/bad.bad log=/path/log.log
    mv temp.tmp ../archive/$FILES
    echo "Load of $FILES complete!" >> ../path/mylog.log
    echo "Deleting remote $FILES" >> /path/mylog.log
    /path/delfile.sh $FILES
    ftp -nvi < delfile.ftp >> ../path/mylog.log
done

This didn't work. It's copying gspp* into $FILES and attempting to execute the rest of the commands.

Code:
for FILES in gspp*
do
echo "Loading $FILES" >> ../logs/myloglog
mv $FILES temp.tmp
...

# 20  
Old 04-05-2007
do you have any of

1. "-f" at end of the #! line at the start of the script
2. set -x in the script
3. IFS=<something> in the script
4. set -o noglob in the script
?
# 21  
Old 04-05-2007
none of that
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Error in looping through files

Hi, I've got a folder with several files I'd like to manipulate. The file names are all ending in .txt and I'd like to loop through their names for manipulation. This is the script I've got so far: for i in 'ls *.gtc.txt|cut -d "." -f1'; do echo${i}; done It should be easy enough, but... (2 Replies)
Discussion started by: zajtat
2 Replies

2. Shell Programming and Scripting

Looping through Files

Hi all , I am new on this forum . I have to face a particoular implementation issue and I need some help . Requirement : I need to read a particoular file (an xml file) and after reading it I need to call an Oracle Stored Procedure passing the content of the file as paramenter , in order... (3 Replies)
Discussion started by: Kolas79
3 Replies

3. Shell Programming and Scripting

Not looping or creating files

So my script is supposed to repeat for every server in my file, but as of now it is getting stuck on my awk commands # Read file cred.txt (with one IP per line), connect to servers (one at a time), and download directory listing i=1 param=$(sed -n "{$1}p" $parm_dir/cdm_param.txt) #Get the last... (6 Replies)
Discussion started by: MJCreations
6 Replies

4. Shell Programming and Scripting

looping through files with different extensions

Hi all, I am trying to make a for loop invoking files with different extensions (*.ugrd and *.vgrd) and I cant just make it work. Cant figure out how to load the files so as to use them in subsequent commands like the ones in this pseudo code. the files are arranged such that in one date for... (8 Replies)
Discussion started by: ida1215
8 Replies

5. Shell Programming and Scripting

looping through files

I am writing a ksh which has to load 7 files(.dat files) from input directory into oracle tables using sql loader. The process has to take each file at a time and once if it is loaded succesfully using sql loader into oracle tables then the process has to pick next file and load it into oracle... (2 Replies)
Discussion started by: vpv0002
2 Replies

6. Shell Programming and Scripting

Help Looping through files in Vi Script

I am trying to write a script that loops through all the files in the current directory that end in '.slg.gz' and runs a parser on each file. Here is my code: #!/bin/bash FILES_HOME = 'dirname $0' for i in $(ls $FILES_HOME/.slg.gz$);do ./run-feature-parser $(i) > OUTPUT.csv done ... (1 Reply)
Discussion started by: kssteig
1 Replies

7. Shell Programming and Scripting

Looping through 2 files simultaneously

Hi all, I'm having a problem with a script which should ultimately provide a filename by reading a value from file1 and file2 then join together. I'm planning to use a loop/ loops to get the values out of both files and create a single string unfortunately the code currently treats the second... (7 Replies)
Discussion started by: chris01010
7 Replies

8. Shell Programming and Scripting

Looping through files...

I posted this in the Solaris forum, but I don't think it's platform specific, so I'm posting it here. Here is the situation. We are a company that has been using a professional publishing system, the software is called "ProType". It runs on Solaris 2.4, however it is no longer supported and we... (6 Replies)
Discussion started by: Fred Goldman
6 Replies

9. Shell Programming and Scripting

Help looping through files, please...

Okay... I've solved one problem. Here's the next. I'm writing a script file that needs to go through a directory and list all files in that directory. I'm using TCL/TK. I figured out how to go through the directory and how to loop through it, but I ran into a little problem. ... (2 Replies)
Discussion started by: kapolani
2 Replies

10. Shell Programming and Scripting

looping files

Hi, I have a file a.lst which lists all files. as a.dat b.dat c.dat I want to process these files mentioned in the list file in a loop. Say I want to display only the first line of all the files a.dat , b.dat, c.dat. How can I go about it? Please help. (5 Replies)
Discussion started by: dharmesht
5 Replies
Login or Register to Ask a Question