Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
I'm new to Shell scripting and I really need to code a tiny script to do the following:
1) Connect via FTP to an FTP server;
2) Download the LIST of all the files in the default folder;
3) FOR EACH file in the LIST, APPEND it's contents to the contents of a bigger file (I want the bigger file to contain all the text files' content in the end);
4) Disconnect from the FTP server.Here's what I've managed to patch together from online tutorials and documents (it doesn't work):
#!/bin/sh
# Script to fetch the all the text files and append them into one big text file.
USER='username'
PASSWD='password'
HOST='IP'
REPORTS_DIR="DIRNAME"# FTP to the text file server
ftp -n $HOST > /tmp/logs/textftp.log 2> /tmp/logs/textftp.log > big.txt
done
quit
SCRIPTEXITSTATUS=$?
if [ $EXITSTATUS != "0" ]
then
# handle the error...
echo 'Failed to connect to FTP or to get the desired files.'
else
# everything's ok...
echo 'Woohoo! We generated the big file!'
fi

For some reason, my script got cut off:
#!/bin/sh
# Script to fetch the all the text files and append them into one big text file.
USER='username'
PASSWD='password'
HOST='IP'
REPORTS_DIR="DIRNAME"# FTP to the text file server
ftp -n $HOST > /tmp/logs/textftp.log 2> /tmp/logs/textftp.log > big.txt
done
quit
SCRIPTEXITSTATUS=$?
if [ $EXITSTATUS != "0" ]
then
# handle the error...
echo 'Failed to connect to FTP or to get the desired files.'
else
# everything's ok...
echo 'Woohoo! We generated the big file!'
fi

I give up, if you could just me a sample script to do the above, please do so asap.
Thanks a million.
P.S. How does one work around the weird script security built into this forum? I can't post my script!

Most likely the message board had problems with "less-than" symbols. In HTML they are the start of a tag e.g. <b>, so if you use one in your post it can mean all the text that follows it is one long invalid tag, and consequently ignored.
You can use HTML's syntax for quoting symbols, but just to keep things interesting this board interprets your posted text twice, once for Preview and again in Confirm, so you have to post something like &lt; or the equivalent &#60;. Check posts 5278 and 5288 where this came up recently.

The easiest way to script ftp is with a .netrc file in your home directory to take care of the log-in, and a here-document, e.g:
ftp somehost <<-ENDFTP
ascii
mget $file1 $file2 $file3
bye
ENDFTPYou could cat all the files together at the end as a separate step.

Thanks for everyone who answered.
The main problem here is not the FTP connection, but rather the CAT in the end and the following facts:
- The number of files to get on the FTP server varies from time to time;
- The name of the files is unknown in advance and must be gotten by doing a FILE LIST on the FTP server (I tried saving it to a file for reading later in a FOR loop, but I could manage to parse it one filename at a time).
Please, email me with any suggestions or code samples for the problem I just mentionned.
Thanks again,
CHRIS

What happens if you do use mget with a wildcard, e.g:
mget x*.dat
Likewise after fetching all the files, why not
cat x*.dat > allfiles.dat

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |