Sponsored Content
Top Forums Shell Programming and Scripting For loop statement - catch error Post 302136760 by cfajohnson on Wednesday 19th of September 2007 02:37:55 PM
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

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Locale::Script(3)					User Contributed Perl Documentation					 Locale::Script(3)

NAME
Locale::Script - standard codes for script identification SYNOPSIS
use Locale::Script; $script = code2script('phnx'); # 'Phoenician' $code = script2code('Phoenician'); # 'Phnx' $code = script2code('Phoenician', LOCALE_CODE_NUMERIC); # 115 @codes = all_script_codes(); @scripts = all_script_names(); DESCRIPTION
The "Locale::Script" module provides access to standards codes used for identifying scripts, such as those defined in ISO 15924. Most of the routines take an optional additional argument which specifies the code set to use. If not specified, the default ISO 15924 four-letter codes will be used. SUPPORTED CODE SETS
There are several different code sets you can use for identifying scripts. A code set may be specified using either a name, or a constant that is automatically exported by this module. For example, the two are equivalent: $script = code2script('phnx','alpha'); $script = code2script('phnx',LOCALE_SCRIPT_ALPHA); The codesets currently supported are: alpha, LOCALE_SCRIPT_ALPHA This is a set of four-letter (capitalized) codes from ISO 15924 such as 'Phnx' for Phoenician. It also includes additions to this set included in the IANA language registry. The Zxxx, Zyyy, and Zzzz codes are not used. This is the default code set. num, LOCALE_SCRIPT_NUMERIC This is a set of three-digit numeric codes from ISO 15924 such as 115 for Phoenician. ROUTINES
code2script ( CODE [,CODESET] ) script2code ( NAME [,CODESET] ) script_code2code ( CODE ,CODESET ,CODESET2 ) all_script_codes ( [CODESET] ) all_script_names ( [CODESET] ) Locale::Script::rename_script ( CODE ,NEW_NAME [,CODESET] ) Locale::Script::add_script ( CODE ,NAME [,CODESET] ) Locale::Script::delete_script ( CODE [,CODESET] ) Locale::Script::add_script_alias ( NAME ,NEW_NAME ) Locale::Script::delete_script_alias ( NAME ) Locale::Script::rename_script_code ( CODE ,NEW_CODE [,CODESET] ) Locale::Script::add_script_code_alias ( CODE ,NEW_CODE [,CODESET] ) Locale::Script::delete_script_code_alias ( CODE [,CODESET] ) These routines are all documented in the Locale::Codes::API man page. SEE ALSO
Locale::Codes The Locale-Codes distribution. Locale::Codes::API The list of functions supported by this module. http://www.unicode.org/iso15924/ Home page for ISO 15924. http://www.iana.org/assignments/language-subtag-registry The IANA language subtag registry. AUTHOR
See Locale::Codes for full author history. Currently maintained by Sullivan Beck (sbeck@cpan.org). COPYRIGHT
Copyright (c) 1997-2001 Canon Research Centre Europe (CRE). Copyright (c) 2001-2010 Neil Bowers Copyright (c) 2010-2013 Sullivan Beck This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.16.3 2013-06-03 Locale::Script(3)
All times are GMT -4. The time now is 03:35 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy