SFTP执行结束后执行Shell脚本的方法及脚本续行异常咨询
Why isn't "Done." printed after SFTP completes?
The most likely culprit is the set -e line in your script. This option tells bash to exit immediately whenever any command returns a non-zero exit status. Even if your file transfer appears to finish successfully, the sftp command might be returning an error code—maybe a hidden file failed to transfer, there was a permission issue on one file, or the remote server sent an unexpected status. Since set -e is enabled, your script stops right after SFTP finishes, never reaching the echo "Done." line.
To confirm this, you can temporarily disable set -e around the SFTP block to capture its exit code:
#!/bin/bash set -e echo "Starting SFTP..." cd "/Users/myuser/myfolder" # Temporarily turn off exit-on-error for SFTP set +e sftp -o "StrictHostKeyChecking no" admin@myserver.myorg.com << END_SCRIPT cd /some/folder/on/remote/host put -r . bye END_SCRIPT sftp_exit_code=$? set -e # Re-enable exit-on-error echo "SFTP exit code: $sftp_exit_code" echo "Done."
Running this will show you if SFTP is indeed returning a non-zero code, which is causing the early exit.
How to execute a shell script after SFTP finishes?
There are a few reliable ways to ensure post-SFTP commands run, depending on whether you want them to execute regardless of SFTP success or only when it works:
1. Run commands regardless of SFTP outcome
If you need your post-SFTP script to run no matter if SFTP succeeded or failed, wrap the SFTP command to avoid triggering set -e, then call your script afterward:
#!/bin/bash set -e echo "Starting SFTP..." cd "/Users/myuser/myfolder" set +e sftp -o "StrictHostKeyChecking no" admin@myserver.myorg.com << END_SCRIPT cd /some/folder/on/remote/host put -r . bye END_SCRIPT set -e # Run your post-SFTP script here ./post_transfer_script.sh echo "Done."
2. Run commands only if SFTP succeeds
Use an if statement to check if SFTP completed successfully before running your post-SFTP steps:
#!/bin/bash set -e echo "Starting SFTP..." cd "/Users/myuser/myfolder" if sftp -o "StrictHostKeyChecking no" admin@myserver.myorg.com << END_SCRIPT cd /some/folder/on/remote/host put -r . bye END_SCRIPT; then echo "SFTP succeeded! Running post-transfer script..." ./post_transfer_script.sh else echo "SFTP failed—skipping post-transfer steps." fi echo "Done."
3. Run cleanup commands even if the script is interrupted
If you need to run commands (like cleaning up temporary files) even if the script is stopped abruptly (e.g., Ctrl+C), use a trap to set up an exit handler:
#!/bin/bash # Define your post-SFTP function post_sftp_steps() { echo "Executing post-SFTP steps..." ./post_transfer_script.sh } # Set trap to run the function when the script exits (for any reason) trap post_sftp_steps EXIT set -e echo "Starting SFTP..." cd "/Users/myuser/myfolder" sftp -o "StrictHostKeyChecking no" admin@myserver.myorg.com << END_SCRIPT cd /some/folder/on/remote/host put -r . bye END_SCRIPT echo "Done."
Bonus Tip
Add the -v flag to your SFTP command (sftp -v -o ...) to get verbose output. This will help you pinpoint exactly why SFTP is returning a non-zero exit code if that's the issue.
内容的提问来源于stack exchange,提问作者Akshay Lokur

