I like to periodically append some data to a remote file via
ssh
and remove it locally. Like:cat some_lines_to_append.txt | ssh user@example.com 'cat >> all_lines_collected.txt' rm some_lines_to_append.txt
Now I like to make sure
some_lines_to_append.txt
is only removed, if the lines where successfully transfered. How to do that?Does
>>
create some sort of error return code by itself on failure, or doescat
in this case, and willssh
deliver that return code?Will
shh
itself deliver non-zero return codes in any occasion that it was finished prematurely?
Answer
cat
will return 0 (zero) on success.
According to ssh
manual :
EXIT STATUS
ssh exits with the exit status of the remote command or with 255 if an error occurred.
So, in your case it is enough
cat some_lines_to_append.txt |
ssh user@example.com 'cat >> all_lines_collected.txt' &&
rm some_lines_to_append.txt ||
echo 'Error occurred.'
Attribution
Source : Link , Question Author : dronus , Answer Author : Alex