我有一个 rsyncs 并像这样结束的脚本
rsync -azvh /dir -e ssh [email protected]:/
RESULT="$?"
# check result of rsync db's
if [ "$RESULT" != "0" ]; then
echo -e "rsync exit Code:" $RESULT "\nFAILED to rsync backups"
else
echo "SUCCESSFULL rsync of backups"
fi
我刚刚被要求将其包装在 API 中,但 API 声明了这一点0=fail
并且1=success
.如何更改退出代码以反映这一点?我需要给它分配一个变量吗?
答案1
exit 1
将退出并返回错误代码 1,exit 0
并将退出并返回错误代码 0。
例如:
rsync -azvh /dir -e ssh [email protected]:/
RESULT="$?"
# check result of rsync db's
if [ "$RESULT" != "0" ]; then
echo -e "rsync exit Code:" $RESULT "\nFAILED to rsync backups"
exit 0
else
echo "SUCCESSFULL rsync of backups"
exit 1
fi
答案2
修改后的答案、报告
false
(或(exit 1)
),并true
根据非标准 API 的需要:rsync -azvh /dir -e ssh [email protected]:/ RESULT="$?" # check result of rsync db's if [ "$RESULT" = "0" ]; then echo "SUCCESSFULL rsync of backups" false else echo -e "rsync exit Code:" $RESULT "\nFAILED to rsync backups" true fi
如果实际上不需要所有文本输出,则可以大大减少代码:
! rsync -azvh /dir -e ssh [email protected]:/
...所需要的只是在前面
rsync
加上一个逻辑 NOT!
并返回与返回值相反的值rsync
。
答案3
通过 bash 超级简单:
test $((`echo "${PIPESTATUS[@]}" | tr ' ' '+'`)) -eq 0
echo $?