GIT,检查命令的返回码(bash 脚本)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31048776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GIT, check return code of commands (bash script)
提问by thahgr
I am working on a large project where it is split in many repositories.
我正在处理一个大项目,它被分成许多存储库。
I am thinking of making a small bash script that iterates and checkouts a specific remote or local branch or tag in each repository, BUTif this fails because the branch doesnt exist, to have a second option of a tag/repository to checkout.
我正在考虑制作一个小的 bash 脚本,它在每个存储库中迭代和检出特定的远程或本地分支或标签,但如果由于分支不存在而失败,则有标签/存储库的第二个选项来检出。
i.e.
IE
#!/bin/bash
printf "\n ### Checkout Tag ### \n \n"
for repo in rep1 rep2 ...
do
checkout
(check if that fails somehow, and if it fails, checkout )
done
printf "\n ### DONE ### \n \n"
exit 0
Or, do you have an alternative idea?
或者,您有其他想法吗?
Thank you
谢谢
采纳答案by Eugeniu Rosca
#!/bin/bash
printf "\n ### Checkout Tag ### \n \n"
for repo in rep1 rep2 ... ; do
checkout
if [[ $? != 0 ]]; then
checkout
if [[ $? != 0 ]]; then
echo "Failed to checkout and "
fi
fi
done
printf "\n ### DONE ### \n \n"
exit 0
回答by Rambo Ramon
You do not need to check the return codes manually. Just concat the commands with ||
and you will be fine
您无需手动检查返回代码。只需将命令连接起来||
就可以了
#!/bin/bash
printf "\n ### Checkout Tag ### \n \n"
for repo in rep1 rep2 ...
do
checkout || checkout || echo "Error"
done
printf "\n ### DONE ### \n \n"
exit 0
||
will execute the following command only if the previous failed.
Think of it as "One of the commands has to succeed". If the first succeeded, you are fine and do not have to check the following.
||
仅当前一个失败时才会执行以下命令。将其视为“命令之一必须成功”。如果第一个成功,您就可以了,不必检查以下内容。
&&
will execute the following command only if the previous succeeded.
Think of it as "All commands have to succeed". If the first one failed, you are already lost and do not have to check the following.
&&
仅当前一个成功时才会执行以下命令。将其视为“所有命令都必须成功”。如果第一个失败,您已经迷路了,不必检查以下内容。
In my opinion, this solution is cleaner and easier than the accepted answer.
在我看来,这个解决方案比公认的答案更清晰、更容易。