dsitech
asked on
Pass last filename from FTP ls to variable
How best to pass the last filename in a list from a FTP session to a variable in a shell script?
Here's what I have done -the solution works, however maybe could be improved.
Run main.sh which calls get-file-list.sh. (.netrc is populated appropriately)
Contents of get-file-list.sh:
ftp whatever.com <<EOT
cd new-files
ls
quit
EOT
Contents of main.sh
./get-file-list.sh >current-list.txt
tail -1current-list.txt >current-last-file.txt
awk '{print $9}'current-last-file.txt
Thanks!
Roger
Here's what I have done -the solution works, however maybe could be improved.
Run main.sh which calls get-file-list.sh. (.netrc is populated appropriately)
Contents of get-file-list.sh:
ftp whatever.com <<EOT
cd new-files
ls
quit
EOT
Contents of main.sh
./get-file-list.sh >current-list.txt
tail -1current-list.txt >current-last-file.txt
awk '{print $9}'current-last-file.txt
Thanks!
Roger
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks,
FILEENTRY & LAST ENTRY are built in variables?
FILEENTRY & LAST ENTRY are built in variables?
ASKER
export is new for me. I looked in "Unix in a Nutshell" and I see that that is how to get a variable to be available to other scripts called by the master script.
I don't quite see how LAST ENTRY gets the value that appears to go into LASTFILE. Could you let me know how that works?
I don't quite see how LAST ENTRY gets the value that appears to go into LASTFILE. Could you let me know how that works?
ASKER
Thanks jkr.
I worked with it a bit and have functional code here:
#/bin/sh
./ftp-whatever-list.sh >whatever_list.txt
export file_list=`cat whatever_list.txt`
export last_file
for file_entry in $file_list; do
last_file=$file_entry
done
echo $last_file > last_file.txt
cat last_file.txt # this is to prove that I can write a log file
I appreciate the example and opportunity to learn a new concept.
Roger
I worked with it a bit and have functional code here:
#/bin/sh
./ftp-whatever-list.sh >whatever_list.txt
export file_list=`cat whatever_list.txt`
export last_file
for file_entry in $file_list; do
last_file=$file_entry
done
echo $last_file > last_file.txt
cat last_file.txt # this is to prove that I can write a log file
I appreciate the example and opportunity to learn a new concept.
Roger
ASKER
Even better (skip the file listing file):
#/bin/sh
export file_list=`./ftp-whatever- list.sh`
export last_file
for file_entry in $file_list; do
last_file=$file_entry
done
echo $last_file > last_file.txt
cat last_file.txt
Thanks again,
Roger
#/bin/sh
export file_list=`./ftp-whatever-
export last_file
for file_entry in $file_list; do
last_file=$file_entry
done
echo $last_file > last_file.txt
cat last_file.txt
Thanks again,
Roger
ASKER
Roger