首页 > 解决方案 > `git worktree remove` 命令是否在失败时返回非零值?

问题描述

我正在编写一个 bash 脚本,我想在其中决定是执行command A还是command B基于命令的返回值 - git worktree remove

说,如果工作树被成功删除,那么我将执行command A. 如果命令中提到的工作树名称错误或由于任何其他原因git worktree remove失败,那么我想执行command B

所以我想出了如下逻辑 -

.
.
.
wt_del=$(git worktree remove -f $DIR)
echo $wt_del                                   ---> for debugging script
if [ $wt_del -eq 0]
then
    git branch
    read -p "Enter the branch : " BUG
    git branch -d $BUG
else
    echo "Failed to remove worktree $DIR"
.
.
.

当我使用无效的工作树名称运行此脚本时,我会看到类似的输出 -wt_del = 所以这意味着该git worktree remove命令没有返回任何整数值来指示成功或失败。那么我该如何做决定呢?

标签: bashgitshell

解决方案


您应该在运行该过程后立即检查退出代码。检查变量$?。如果不为 0,则有错误。

git blahblah
if [ $? -ne 0 ]; then
    echo there was a problem
fi

推荐阅读