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
To check the exit code we can simply print the $? special variable in bash. This variable will print the exit code of the last run command. $ echo $?
To display the exit code for the last command you ran on the command line, use the following command: $ echo $? The displayed response contains no pomp or circumstance. It's simply a number.
Run the rpm -q ftp command to see if the ftp package is installed. If it is not, run the yum install ftp command as the root user to install it. Run the rpm -q vsftpd command to see if the vsftpd package is installed. If it is not, run the yum install vsftpd command as the root user to install it.
Close an ftp connection to a remote system by using the bye command. ftp> bye 221-You have transferred 0 bytes in 0 files. 221-Total traffic for this sessions was 172 bytes in 0 transfers.
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.
200 PORT command successful.
150 Opening ASCII mode data connection for filename.
226 Transfer complete.
189 bytes sent in 0.145 seconds (0.8078 Kbytes/s)
Here's a sample script.
FTPLOG=/temp/ftplogfile
ftp -inv <<! > $FTPLOG
open server
user ftp pwd
put filename
close
quit
!
FTP_SUCCESS_MSG="226 Transfer complete"
if fgrep "$FTP_SUCCESS_MSG" $FTPLOG ;then
echo "ftp OK"
else
echo "ftp Error: "$OUT
fi
exit 0
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):
EXIT STATUS
Wget may return one of several error codes if it encounters problems.
0 No problems occurred.
1 Generic error code.
2 Parse error---for instance, when parsing command-line options, the .wgetrc or .netrc...
3 File I/O error.
4 Network failure.
5 SSL verification failure.
6 Username/password authentication failure.
7 Protocol errors.
8 Server issued an error response.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With