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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using for...do...done in a while loop 1

Status
Not open for further replies.

ryanc2

MIS
Apr 18, 2003
73
US
Having problems trying to use for...do...done in a while loop.

cat control.file
file1:directory:##.##.##.#:user:password
file2:directory:##.##.##.#:user:password
file3:directory:##.##.##>#:user:password

#!/bin/ksh
#
#Read variables from a control file for FTP input for list of different files in different directories
IFS=:
exec < control.file
while read file l_dir ip id pw
do
cd $directory
ls files* > list_files

#FTP to get list of files already sent
ftp -ni ip <<-EOF
user id pw
dir files* directory/sent_files
quit
EOF

#Compare local list with sent list to derive good_files
comm -23 list_files sent_files > good_files

#FTP each file in good_files list
for f in `cat good_files`
do
ftp -ni ip <<-EOF
user id pw
put $f
quit
EOF
#Move each file in good file list to save directory
mv $f directory/save
done
done

PROBLEM: the for f in `cat ...` reads each file on one string instead of individually.

I need this:
mv good_file1 directory/save
mv good_file2 dierctory/save

Instead it reads all the files at once instead of individually.


CAN ANYONE HELP ME???

Thanks!!


 

try this one:

# remove the IFS before the 'exec'

#Read variables from a control file for FTP input for list of different files in different directories

exec < control.file
while IFS=&quot;:&quot; read file l_dir ip id pw

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks for the quick response.

I'm still having the same result as before.

contents of good_files:
WASORAL101137.DAT
WASORAL101138.DAT

The script result:

mv: cannot access /du0/wms/export/order/WASORAL101137.DAT
WASORAL101138.DAT: No such file or directory (error 2)

 
# you're changing the IFS at the beginning of the script -
# THAT affect any subsequent &quot;read&quot;
# You can leave the logic as is (if you want) and change
# the way you read from the 'good_files' file
#

#FTP each file in good_files list
while f=$(line)
do
ftp -ni ip <<-EOF
user id pw
put $f
quit
EOF
#Move each file in good file list to save directory
mv $f directory/save
done < good_files

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Just curious...what does the f=$(line) do? Also, does this replace the for f in `cat good_files.tmp` line?
 
# read the entire line in a variable 'f'
f=$(line)

> Also, does this replace the for f in `cat good_files.tmp` line?

Your 'line reading' method has changed from using 'for f in ...' TO 'while f=$(line)...'.

The 'for f in ...' used the value of the IFS as it iterated through the list [in your case the output of `cat good_files.tmp`]. You reset your IFS at the top of script to be ':'.

The 'while f=$(line)' reads the ENTIRE line of 'good_files.tmp'. The rest of the loop assumes each line contained a SINGLE file.

'man ksh'
'man line'

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top