Reputation: 2543
I have a simple "main" shell script that does a few prep things and then calls another shell script that uploads a file to an ftp site. I'd like to know how I can wait and check the exit code of the called shell script and also how I could easily check whether the FTP file was actually successfully uploaded and provide a proper exit code (0 or 1)
thank you
main script:
#!/bin/sh
# check for build tools first
FTP_UPLOAD_SCRIPT=~/Desktop/ftp_upload.sh
if [ -f "$FTP_UPLOAD_SCRIPT" ]; then
echo "OK 3/5 ftp_upload.sh found. Execution may continue"
else
echo "ERROR ftp_upload.sh not found at $FTP_UPLOAD_SCRIPT. Execution cannot continue."
exit 1
fi
# upload the packaged installer to an ftp site
sh $FTP_UPLOAD_SCRIPT
# check the ftp upload for its exit status
ftp_exit_code=$?
if [[ $ftp_exit_code != 0 ]] ; then
echo "FTP ERRORED"
exit $ftp_exit_code
else
echo $ftp_exit_code
echo "FTP WENT FINE"
fi
echo "\n"
exit 0
ftp_upload_script:
#!/bin/sh
FTP_HOST='myhost'
FTP_USER='myun'
FTP_PASS='mypass'
FTPLOGFILE=logs/ftplog.log
LOCAL_FILE='local_file'
REMOTE_FILE='remote_file'
ftp -n -v $FTP_HOST <<SCRIPT >> ${FTPLOGFILE} 2>&1
quote USER $FTP_USER
quote PASS $FTP_PASS
binary
prompt off
put $LOCAL_FILE $REMOTE_FILE
bye
SCRIPT
echo $!
Upvotes: 0
Views: 4276
Reputation: 2543
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
Upvotes: 0
Reputation: 535
I think what you're looking for is exit $?
instead of echo $!
at the bottom of your FTP script.
Using echo
will simply print to stdout
but will not return an exit code (thus exit
should be used). The special $?
is the return code of the previous process, not $!
, which is the process ID.
Upvotes: 3