bash - 认识到我不在带有 bash 的 git 标签上

标签 bash git shell

我想要一个 bash 脚本来识别调用者是否不在 git 标签上(但在未标记的分支上)。我尝试了以下方法:

OUTPUT=$(git describe --exact-match --tags $(git log -n1 --pretty='%h')|grep "fatal:")
if [[ $OUTPUT != "0" ]]; then
    echo "no tag present"
fi

但我得到 no tag present 即使显然有一个标签并且 git describe --exact-match --tags $(git log -n1 --pretty ='%h') 不包含 fatal:。为什么这不能按预期工作?

最佳答案

如果更容易依赖该命令的退出状态:

if ! git describe --exact-match --tags $(git log -n1 --pretty='%h') 1>/dev/null 2>&1; then
   echo "no tag present"
fi

如果没有标签,退出状态为128。
如果存在则存在状态为0。

jthill建议 in the comments :

if msg=`git describe --exact-match --tags @ 2>&1`; then 
    echo tagged $msg; 
else 
    echo not tagged; 
fi

关于bash - 认识到我不在带有 bash 的 git 标签上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62706703/

相关文章:

linux - BEGIN 和 END 在 gawk 中导致语法错误

mysql - 在终端中执行 MySQL 命令时出现错误 1054 (42S22)

bash - Linux Bash Shell 自定义错误消息

linux - docker运行命令错误

Bash:替换部分文件名

linux - Bash 脚本错误 0=1 : command not found

linux - 如何获取 gerrit 评论 ID 或链接?

git - 如何使用Github Release API 发布没有源代码的版本?

git merge 分支像 pull 链一样时尚

linux - 从 bash 将特定的十六进制模式写入文件