Getting Exit Status Code from 'Ftp' Command in Linux Shell

Getting exit status code from 'ftp' command in linux shell

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

how to check for an external shell script exit code

I have come up with a solution that both double checks the ftp upload and then provides an appropriate exit code.

... ftp upload first ... then ...

# this is FTP download double-check test
ftp -n -v $FTP_HOST <<SCRIPT >> ${FTPLOGFILE} 2>&1
quote USER $FTP_USER
quote PASS $FTP_PASS
binary
prompt off
get $REMOTE_FILE $TEST_FILE
bye
SCRIPT

#check to see if the FTP download test succeeded and return appropriate exit code
if [ -f "$TEST_FILE" ]; then
echo "... OK FTP download test went fine. Execution may continue"
exit 0
else
echo "... ERROR FTP download test failed. Execution cannot continue"
exit 1
fi

Checking ftp return codes from Unix script

The ftp command does not return anything other than zero on most implementations that I've come across.

It's much better to process the three digit codes in the log - and if you're sending a binary file, you can check that bytes sent was correct.

The three digit codes are called 'series codes' and a list can be found here

check existence of ftp file and parse exit code

How about using curl?

 curl -I --silent ftp://username:passwd@192.168.1.63/filenotexist.txt >/dev/null

$? is 0 if file exists,
$? is not 0 if file doesn't exists.

Get Return Code of SFTP command

What I would do :

echo "sftp start" >> /test/logfile.log

sftp user@server <<EOF >> /test/logfile.log
cd /tgt/files
lcd /src/files
rm *.csv
put -p *.csv
exit
EOF

exit_code=$?

if [[ $exit_code != 0 ]]; then
echo "sftp error" >&2
exit 1
fi

echo "sftp end" >> /test/logfile.log

What is the best EXIT status check Command that works in Rubocop, Linux and Windows servers?

The accepted way is:

require 'English'  # Capital 'E'!

$CHILD_STATUS.exitstatus

Note that the English lib is a standard lib that comes bundled with all versions of Ruby

127 Return code from $?

Value 127 is returned by /bin/sh when the given command is not found within your PATH system variable and it is not a built-in shell command. In other words, the system doesn't understand your command, because it doesn't know where to find the binary you're trying to call.

Incorrect exit status being fetched after executing a script

To get the exit code of your script Import.sh instead of its output, change the script temp.sh to

/app/arjun/scripts/Import.sh schema_remap_AANV02_UAT2.txt ARJSCHEMA
RC=$?
echo "Return code = $RC"

or simply

/app/arjun/scripts/Import.sh schema_remap_AANV02_UAT2.txt ARJSCHEMA
echo "Return code = $?"

See the comments for hints how to fix/improve your scripts.



Related Topics



Leave a reply



Submit