I have some script to be executed on remote server(server-2) through ssh from server-1 and I have to write that output into a log file named file.log on server-1.
I am trying this: sc.sh
echo 'testing'
cp $HOME/dir1/file1 $HOME/dir2
Now, executing the sc.sh through ssh:
sshpass -p 'psswd' ssh username@server-2 "bash -s" < sc.sh | tee -a file.log
if [ $? -eq 0]; then
echo "successfully executed the script file"
.
.
.
else
echo "failed copying due to incorrect path"
fi
Now because of tee -a file.log command, it will always return 0 even though my commands in script file fails. How can I write into log file and should check the if condition after ssh command which should work based on the ssh commands exit code?
man bash->PIPESTATUS. Also seepipefail.