For loop statement - catch error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting For loop statement - catch error
# 1  
Old 09-19-2007
For loop statement - catch error

I'm having a question about for loops. (bash)

I have the following for example:

for file in `ls *.txt`
do
read file ...
done

Now when there is a file present there is no problem, now when there is no file present I get the following output in my standard mail box : "No such file or directory" Script is executed via crontab.

Now I want to catch the above error so I don't get it in my mail any more, but I have no idea how to do this.

I can make an if statement first "if [ -f *.txt ] ...", but there must be a better solution.

Thx.
# 2  
Old 09-19-2007
if

I think the use of the 'if' statement is a perfectly reasonable solution. You need some kind if conditional statement to determine whether or not your file exists and an 'if' would be OK
# 3  
Old 09-19-2007
Quote:
Originally Posted by lumdev
I can make an if statement first "if [ -f *.txt ] ...", but there must be a better solution.
Check for the actual file in the loop

Code:
  for file in *.txt
  do
     if test -f $file
     then
        read file ...
     fi
  done

# 4  
Old 09-19-2007
Quote:
Originally Posted by lumdev
I'm having a question about for loops. (bash)

I have the following for example:

for file in `ls *.txt`

That is not the way to loop through files. Not only is ls unnecessary, it will break your script if any filenames contain spaces or other pathological characters. Use the wildcard directly:

Code:
for file in *.txt

Quote:
do
read file ...
done

Now when there is a file present there is no problem, now when there is no file present I get the following output in my standard mail box : "No such file or directory" Script is executed via crontab.

Now I want to catch the above error so I don't get it in my mail any more, but I have no idea how to do this.

I can make an if statement first "if [ -f *.txt ] ...", but there must be a better solution.

No, you cannot do that; it will fail if there is more than one .txt file.

You can use a function:

Code:
is_file() {
   test -f "$1"
}

is_file *.txt &&
 for file in *.txt
 do
   ...
 done

The safest way is to check each file:

Code:
for file in *.txt
do
  [ -f "$file" ] || continue
  ...
done

# 5  
Old 09-20-2007
Thx for the answers.

I used the solution with the function, now I don't get any "No such file or directory" output anymore.

Thx for the help cfajohnson.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to catch sql error in script?

Hi Gurus, I have a script which send sql query to oracle db and return value to my script. dummy code like below: sqlplus -s user/${PASSWD}@${ORACLE_SID} @${DIR}/query.sql > outputfile using above code, when query has error, it send error to same out put file and exit code is 0, is... (6 Replies)
Discussion started by: ken6503
6 Replies

2. Shell Programming and Scripting

[Solved] FOR loop / IF statement returning error

The code at the bottom is a simplified example of what we have. If I use the following: && echo "echo failed" $? returns 1 When I use if ; then echo "echo failed" ; fi $? returns 0 Does anyone know what's wrong with this? Using AIX 6.1 and KSH for NUM in 1 2 3 do ... (5 Replies)
Discussion started by: jfxdavies
5 Replies

3. Shell Programming and Scripting

While loop within if statement

Hi, I'm a rookie who is trying to learn this stuff. What I need help with is putting together a non complicated "while" loop within the below "if" statement. I also need the while loop to keep looping until the user types a key to end the loop. Please reveal the proper insertion points. Thank... (4 Replies)
Discussion started by: jefferj54
4 Replies

4. Shell Programming and Scripting

Unable to catch the redirection error when the disk is full

Hi Experts, Problem summary : I am facing the below problem on huge files when the disk is getting full on the half way through the execution. If the disk was already full , the commands fail & everything is fine. Sample Code : head_rec_data_file=`head -1 sample_file.txt` cat... (9 Replies)
Discussion started by: Pruthviraj_shiv
9 Replies

5. Shell Programming and Scripting

Python rrdtool try catch statement

I have a Python script that updates an rrd file with data it pulls from a database. Here is a snippet of the Python code for i in range(numrowed): row = curred.fetchone() time_stamp = row rx_max = row ret =... (1 Reply)
Discussion started by: kaf3773
1 Replies

6. Shell Programming and Scripting

for loop with internal unix command in statement throwing error

Hi I've gotten a plugin script that won't run. I keeps throwing an error at the following line. for BARCODE_LINE in `cat ${TSP_FILEPATH_BARCODE_TXT} | grep "^barcode"` do #something done The error reads ... (3 Replies)
Discussion started by: jdilts
3 Replies

7. Shell Programming and Scripting

Catch error from SFTP session

We have script running to SFTP some file to the remote server. The problem is the SFTP transfer returns an exit code of 0 even if there is permission error during file transfer, connection refuse (like when sftp server is down), thus, returning the status of the script as success. I was thinking... (3 Replies)
Discussion started by: The One
3 Replies

8. Programming

C - advice how to catch some weird error

I have some unstable mistake in my program and out-of-idea how to catch it. I am looking for advice with a way to work it out! I have in a pretty complicated program (but one source file) set of int-counters - 15, if exactly. Lately, on final printout I have inpossible value (I am... (3 Replies)
Discussion started by: alex_5161
3 Replies

9. Shell Programming and Scripting

Unable to catch the output after core dump and bus error

I have a weird situation in which the binary dumps core and gives bus error. But before dumping the core and throwing the buss error, it gives some output. unfortunately I can't grep the output before core dump db2bfd -b test.bnd maxSect 15 Bus Error (core dumped) But if I do ... (4 Replies)
Discussion started by: rakeshou
4 Replies

10. UNIX for Dummies Questions & Answers

if statement in a while loop

#!/usr/bin/ksh echo Please enter while read n do echo $n >> datafile done question: How can I enject an if statement that if the users enter 0 (zero) the program will exit? this is what I have but not working #!/usr/bin/ksh echo Please enter number while read n do if $n=0 then... (2 Replies)
Discussion started by: bobo
2 Replies
Login or Register to Ask a Question