Solved

Pass last filename from FTP ls to variable

Posted on 2001-07-07
6
710 Views
Last Modified: 2013-12-26
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

0
Comment
Question by:dsitech
  • 5
6 Comments
 

Author Comment

by:dsitech
ID: 6261609
Actually, I just have the result printing to the screen in the above example - still need to pass that to a variable. After that, I would use the variable to actually get the file.

Roger
0
 
LVL 86

Accepted Solution

by:
jkr earned 60 total points
ID: 6261762
If the file just contained the filenames line by line, you could do that like

export FILELIST=`cat current-list.txt`
export LASTENTRY
for FILEENTRY in $FILELIST; do
LASTFILE = FILEENTRY
done

After the loop, 'LASTENTRY' will hold the last file's name.
0
 

Author Comment

by:dsitech
ID: 6262095
Thanks,

FILEENTRY & LAST ENTRY are built in variables?

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:dsitech
ID: 6262100
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?

0
 

Author Comment

by:dsitech
ID: 6262135
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
0
 

Author Comment

by:dsitech
ID: 6262140
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Process filename extension 3 172
sumNumber challenge 16 111
matchUp  challenge 9 94
sumHeights  challenge 17 66
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now