简体   繁体   English

GIT,检查命令的返回码(bash脚本)

[英]GIT, check return code of commands (bash script)

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, BUT if this fails because the branch doesnt exist, to have a second option of a tag/repository to checkout. 我正在考虑制作一个小的bash脚本,以迭代并签出每个存储库中的特定远程或本地分支或标签, 但是如果由于分支不存在而失败,则有一个标签/存储库的第二个选项来签出。

ie

#!/bin/bash
printf "\n ### Checkout Tag ### \n \n"

for repo in rep1 rep2 ...
do

checkout $1 
(check if that fails somehow, and if it fails, checkout $2)

done

printf "\n ### DONE ### \n \n"

exit 0

Or, do you have an alternative idea? 或者,您有其他选择吗?

Thank you 谢谢

#!/bin/bash
printf "\n ### Checkout Tag ### \n \n"

for repo in rep1 rep2 ... ; do
    checkout $1
    if [[ $? != 0 ]]; then
        checkout $2
        if [[ $? != 0 ]]; then
            echo "Failed to checkout $1 and $2"
        fi
    fi
done

printf "\n ### DONE ### \n \n"
exit 0

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 $1 || checkout $2 || 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. 我认为,此解决方案比公认的答案更干净,更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM