Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FOR and DO Loop Help 2

Status
Not open for further replies.

wellster34

Programmer
Sep 4, 2001
113
CA
Hi,

I know very little about Unix and was given a script which is giving me trouble. I need help in understanding what is going on in this piece of Unix script. It is a Korn shell script.

for f in sourcename.txt
do
if [ "$f" = "sourcename.txt" ]; then
echo "****************************************************"
echo "* No files found to build data file. *"
echo "****************************************************"
rm -f targetname.txt
exit 1
else
echo " Appending file $f to targetname.txt."
RC0=`cat $f >> targetname.txt`
RCANS0="$?"
if [ $RCANS0 -eq 0 ]; then
echo " Deleting file $f."
RC1=`rm -f $f `
RCANS1="$?"
if [ $RCANS1 -eq 0 ]; then
echo " "
else
#
# Indicate the delete file process completed unsuccessfully.
#
echo " "
echo "***********************************************"
echo "* Could not delete the datetime stamped file. *"
echo "***********************************************"
echo " "
echo " --> Finished FTP process unsuccessfully."
echo " --> End of Step 02 "
exit 1
fi
else
#
# Indicate the append file process completed unsuccessfully.
#
echo " "
echo "***********************************************"
echo "* Could not append the datetime stamped file. *"
echo "***********************************************"
echo " "
echo " --> Finished FTP process unsuccessfully."
echo " --> End of Step 02 "
exit 1
fi
fi
done

I need help in understanding the for do loop. Can someone explain what is going on above in the code?

Anything at all will be greatly appreicated!

Thanks,
Josh
 
It looks like it is intending to process a list of files that is contained in "sourcename.txt". However, the code above will not do that. The line:

Code:
for f in sourcename.txt[code]

will assign the value "sourcename.txt" to the variable "f", and only process the loop once.

Maybe what you want is:

[code]for f in $(cat sourcename.txt)[code]

which will assign each filename in sourcename.txt to the variable f, and do the loop processing each one.
 
The sourcename.txt is a file that contains a list of data. I tried the idea above and got a new error:


Appending file 002476020402000001720000000000000001007 to /library/file_transfer/devl/upload/IOL_AP010_01.dat.

cat: 0652-050 Cannot open 002476020402000001720000000000000001007.


The 002476020402000001720000000000000001007 is the first record of information in the sourcename.txt.

It appeared to fail on RC0=`cat $f >> targetname.txt`
because it had the cat problem.

I need more help to solve this problem. Any other ideas/suggestions.
 
Oops the /library/file_transfer/devl/upload/IOL_AP010_01.dat is actually the targetname.txt.
 
Okay, lets start with a fuller explanation of dobbyn's comment:
the "for" command sets its variable to each entry in it's argument list (but not neccessarily in order!)
for example:

for x in one two three four
do
echo $x
done

produces the output:

one
two
three
four

(although not necessarity in that order).

So what is happening in your original loop is that the value of f is set at the start to "sourcename.txt". The "if" statement immediately following tests for this, so it gives you the "No Files found" message, deletes the file targetname.txt, and exits the script with error code "1".
The rest of the script is never executed.

Dobbyn assumed (with good reason) that the intent was that f should loop through the contents of the file sourcename.txt, which, judging by the rest of the script, would be a list of filenames to be concatenated.
$(cat sourcename.txt) is the same as `cat sourcename.txt` (except for a few technicalities). When the shell see it, it places the entire contents of the file sourcename.txt, with the newline characters replaced by spaces, on the text line in place of the $(cat sourcename.txt). Then it interprets the rest of the line. For example:

if the file testfile.txt contains the lines
aa bb
cc dd
ee

when UNIX sees

for x in $(cat testfile.txt)

it first changes the line into

for x in aa bb cc dd ee

and then executes the for loop as above. (Forgive me if I am telling you things you already know. I've found it's better to over explain, than to under explain.)

Unfortunately, that is not what you wanted at all. sourcename.txt is not a list of file names and the script runs into trouble when it tries to open a file named 002476020402000001720000000000000001007, since obviously no such file exists.

I am not certain what was intended here. As the script is, it will never do anything other than give the "No files found" message and exit. My best guess is that whoever set this up also created another script which he ran first. That script would create a copy of this one, and replace the "sourcename.txt" in the for statement with a list of all the files needing processed. Then the copy would be run. The if statement after the for is there to stop the program when the someone tries to run the original instead of the copy.

This is a very convoluted way of going about it, but I've seen worse, and from the looks of the remainder of the script, whoever wrote it likes to do things the hard way!

I notice the other error messages refer to this as "step 2".
Try looking at whatever was "step 1". My guess is you will find that it modifies this script.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top