ssh | 如何处理多个命令的错误

ssh | 如何处理多个命令的错误

嗨,我知道我们可以通过 $? 检查上一个命令的状态,以下场景对我来说不起作用

ssh $USER@$HOST bash -c "'
echo "Preparing list of files to be transferred...."
rsync <something>
if [ $? -ne 0 ]; then
    echo "remote error occurred"
    exit 1
fi
echo "end"
'"
if [ $? -ne 0 ]; then
    echo "remote error occurred"
    exit 1
else
    echo "remote did not return any error"
    exit 0
fi

我得到的输出是

Preparing list of files to be transferred....
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6]
end
remote did not return any error

关于如何处理 ssh 上多个命令的错误有什么建议吗?

答案1

我想我找到答案了

rsync <something>
if [ \$? -ne 0 ]; then
    echo "remote error occurred"
    exit 1
fi

添加反斜杠即可解决问题。

相关内容