Проверьте успешность добавления удаленного файла через ssh

248
dronus

Мне нравится периодически добавлять некоторые данные в удаленный файл через sshи удалять его локально. Подобно:

cat some_lines_to_append.txt | ssh user@example.com 'cat >> all_lines_collected.txt' rm some_lines_to_append.txt 

Теперь мне нравится, чтобы убедиться, что some_lines_to_append.txtудаляется только, если строки были успешно переданы. Как это сделать?

Создает ли >>какой-либо код возврата ошибки сам по себе в случае сбоя или создает catв этом случае и sshдоставит ли этот код возврата?

Будет ли shhсамо по себе доставлять ненулевые коды возврата в случае преждевременного завершения?

0

1 ответ на вопрос

1
Alex

cat вернет 0 (ноль) в случае успеха.

Согласно sshинструкции:

СТАТУС ВЫХОДА

 ssh exits with the exit status of the remote command or with 255 if an error occurred. 

Итак, в вашем случае достаточно

cat some_lines_to_append.txt | ssh user@example.com 'cat >> all_lines_collected.txt' && rm some_lines_to_append.txt || echo 'Error occurred.' 
Cool. Also someone else told me `ssh` would in fact pass the return code of the invoked commands. So maybe the pipe would just work without capturing the return code via `echo $?`and `rc=...`? dronus 7 лет назад 0
It seems just `cat some_lines_to_append.txt | ssh user@example.com 'cat >> all_lines_collected.txt' && rm some_lines_to_append.txt` would just work better then first expected by me... dronus 7 лет назад 0
Yes, `ssh` exits with the exit status of the remote command or with 255 if an error occurred. rc=$(... code needed when more complex remote commands executed and you want to catch some errors in a middle, but in your case `cat some_lines_to_append.txt | ssh user@example.com 'cat >> all_lines_collected.txt' && rm some_lines_to_append.txt || echo 'Error occurred'` is enough. Alex 7 лет назад 0
Я отредактировал свой ответ, чтобы он отражал ваши конкретные потребности без лишней ненужной информации. Alex 7 лет назад 0
Здорово. Иногда первый выстрел работает хорошо, несмотря на ожидание. dronus 7 лет назад 0

Похожие вопросы