Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 $!
share|improve this question
    
echo $! so you output the return value from the ftp command, instead of just a 0? –  Marc B Nov 20 '12 at 18:47
    
The ftp_upload_script is broken! It always exits 0, indicating that it succeeded. Just remove the exit 0, and it will return the exit status of ftp, which presumably fails if it does not upload the file. (I haven't used ftp in years, and do not know if it reliably reports its status. Use scp instead.) –  William Pursell Nov 20 '12 at 18:49
    
I've tried to output echo $! but it returns (afaict) 0, even if the fpt script fails to connect to the host (I've changed the host IP so it couldn't). I'm reading the exit value with ftp_exit_code=$? and then printing it with exit $ftp_exit_code in the main script. I've updated the above scripts to incorporated your suggestions. ....I'm not allowed to use scp unfortunately (not my decision) –  trekme Nov 20 '12 at 18:57
    
Since the echo $! 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 the echo. –  Jonathan Leffler Nov 21 '12 at 17:50

2 Answers 2

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.

share|improve this answer
    
Note that the shell exits with the status of the last command executed. You can also use exec ftp ... to replace the shell script with the FTP command and then you're guaranteed to get the exit status of FTP. My experience with FTP is that it returns 0 (success) regardless of what went wrong, so testing the result isn't much help anyway, but for the general case where the command exit status is of relevance, either of these (exec or simply exit) will help. –  Jonathan Leffler Nov 21 '12 at 17:48
    
yeah, that's what I've noticed (always 0 even after an error). that's why I've added the download double check test right after the upload. I think it's a valid test to confirm that your file was actually uploaded in the first place. –  trekme Nov 21 '12 at 17:54
up vote 0 down vote accepted

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.