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 $!
echo $!
so you output the return value from the ftp command, instead of just a 0? – Marc B Nov 20 '12 at 18:47ftp_upload_script
is broken! It always exits 0, indicating that it succeeded. Just remove theexit 0
, and it will return the exit status offtp
, which presumably fails if it does not upload the file. (I haven't usedftp
in years, and do not know if it reliably reports its status. Usescp
instead.) – William Pursell Nov 20 '12 at 18:49echo $!
succeeds (even if there is no background PID to report), the exit status of the script will be 0, the same as the exit status of theecho
. – Jonathan Leffler Nov 21 '12 at 17:50