bash - `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
命令没有返回任何整数值来指示成功或失败。那么我该如何做决定呢?
解决方案
您应该在运行该过程后立即检查退出代码。检查变量$?
。如果不为 0,则有错误。
git blahblah
if [ $? -ne 0 ]; then
echo there was a problem
fi
推荐阅读
- mongodb - 如何在mongodb中声明主键?在不知道它的价值的情况下创建索引
- powershell - 检查字符串在 PowerShell 中是 Base64 编码的
- python - 创建对象时未添加 Django created_at 字段
- jquery - 在jQuery中选择带有文本模式和数字的选择框
- java - ComputableFuture 并行调用
- objective-c - 设置 UITextField 约束以在换行符中继续
- swift - 错误:协议只能用作通用约束,因为它具有 Self 或关联的类型要求
- css - Angular FlexLayout - 子 div 在扩展父 div
- java - Java Swing 组件中老挝语文本的显示问题
- uwp - 如何在 Uno 平台上使用字体图标