I need to retrive the exit status code from a command line program. No worries, I used $?. But for ftp, even if it doesn't connect, it opens the ftp shell, so I'm not able to understand that the connection haven't take place.
Try this code for understand:
#!/bin/sh
ftp 1234567
OUT=$?
if [ $OUT -eq 0 ];then
echo "ftp OK"
else
echo "ftp Error: "$OUT
fi
exit 0
Any help? Thanks Filippo
You should be looking for success message from ftp command rather than looking for a status. It's "226 Transfer complete". You can confirm it with ftp manual on your system.
Here's a sample script.
If you need to download something and see if the download succeeded, why don't you use wget? It supports the FTP protocol.
It will report the status of the download with several return codes (quote from man page):
some scripts do -
Except that the above doesn't always work - most FTP clients always exit with a status of 0. This leads to ugly "false negatives": the file transfer fails, but the script doesn't detect the problem.
One way to verify that a file transfer took place - transfer it back:
Another way around this is to check if you have the file on your server post transfer!
Something like...
If it's not there then it didn't transfer successfully!
The last time I needed to use ftp in a script, I got so frustrated with it that I finally found a BSD-licensed ftp client source and simply modified it to give it the behavior I needed, and used that instead of the version provided with the OS.
Ugly, but the depth of head-level dents in the cube wall was starting to get ridiculous
Try the following scripts.
To copy:
To move:
Here are some test cases:
For copying.
For moving.
Update: Remember to read man 5 netrc