Search code examples
linuxshscp

Will a failed scp transfer exit my script?


I have a shell script I am running which basically tars up log files and scp transfers the tar off the host to a remote host.

If the transfer is successful, on the next line of the script I want to purge my logs. If the transfer fails i would not want to purge them.

My question is, if the scp fails for whatever reason (poor GSM connectivity), will the script terminate at the fail of the scp, and naturally not run the purge on the next line, or do i need to add something in for protection if the scp fails to not purge the logs??? Cheers.

scp -P $port /tmp/$filename user@host:/home/user/logs/
rm /home/user/logs/*

Solution

  • The idiomatic way to obtain this behavior is to write it as

    scp ... || exit 1