有没有一种简单的方法来测试bash中一组命令的退出状态?
问题描述:
我有一个简单的脚本来从远程服务器获取数据的过程中它使用rsync
产生:有没有一种简单的方法来测试bash中一组命令的退出状态?
while :
do
rsync -avz --remove-source-files -e ssh [email protected]:path/to/foo* ./
rsync -avz --remove-source-files -e ssh [email protected]:path/to/bar* ./
rsync -avz --remove-source-files -e ssh [email protected]:path/to/baz* ./
rsync -avz --remove-source-files -e ssh [email protected]:path/to/qux* ./
sleep 900 #wait 15 minutes, try again
done
如果没有文件,rsync
返回退出状态12(显然)。如果无上面的调用rsync
找到任何数据,我想打破循环(生成数据的过程可能已退出)。为了减轻任何困惑,我做了而不是想要从循环中突破,如果即使是1的rsync
进程成功。
在bash中有一个简洁的方法吗?
答
这种方式计算由于没有文件而失败的次数。
while :
do
nofile=0
rsync -avz --remove-source-files -e ssh [email protected]:path/to/foo* ./
(($? == 12)) && let nofile++
rsync -avz --remove-source-files -e ssh [email protected]:path/to/bar* ./
(($? == 12)) && let nofile++
rsync -avz --remove-source-files -e ssh [email protected]:path/to/baz* ./
(($? == 12)) && let nofile++
rsync -avz --remove-source-files -e ssh [email protected]:path/to/qux* ./
(($? == 12)) && let nofile++
# if all failed due to "no files", break the loop
if (($nofile == 4)); then break; fi
sleep 900 #wait 15 minutes, try again
done
答
您可以通过添加了返回值,所以,如果他们都返回12,总和是48做到这一点:
while :
do
rc=0
rsync -avz --remove-source-files -e ssh [email protected]:path/to/foo* ./
let rc+=$?
rsync -avz --remove-source-files -e ssh [email protected]:path/to/bar* ./
let rc+=$?
rsync -avz --remove-source-files -e ssh [email protected]:path/to/baz* ./
let rc+=$?
rsync -avz --remove-source-files -e ssh [email protected]:path/to/qux* ./
let rc+=$?
if [[ $rc == 48 ]]; then # 48 = 4 * 12
break;
fi
sleep 900 #wait 15 minutes, try again
done
请注意,如果你得到的返回码和的另一种组合可能遭受48,即0 + 0 + 12 + 36
答
由其他答案的启发,我觉得这是我能做到这一点迄今最清晰的方式...
while :
do
do_continue=0
rsync -avz --remove-source-files -e ssh [email protected]:path/to/foo* ./ && do_continue=1
rsync -avz --remove-source-files -e ssh [email protected]:path/to/bar* ./ && do_continue=1
rsync -avz --remove-source-files -e ssh [email protected]:path/to/baz* ./ && do_continue=1
rsync -avz --remove-source-files -e ssh [email protected]:path/to/qux* ./ && do_continue=1
if [[ $do_continue == 0 ]]; then
break
fi
sleep 900 #wait 15 minutes, try again
done
这可能进行重构,以去除break语句和相关条件测试:
do_continue=1
while [ do_continue -eq 1 ]; do
do_continue=0
rsync -avz --remove-source-files -e ssh [email protected]:path/to/foo* ./ && do_continue=1
#...
sleep 900
done
退出状态12似乎与协议数据流中的错误有关。你想退出任何*非零退出状态(来自所有'rsync'进程)的循环吗? – ezod
@ezod - 我认为任何非零退出状态都足够让我打破(但只有当它们都不为零时)。 – mgilson