检查bash脚本中的错误

布莱恩·麦克基

${deleteOldBranchRemote}没有错误执行时,如何修改此脚本以有条件地执行${getRename}

Now_hourly=$(date +%d%b%H%M)
#echo "$Now_hourly"

newrcName="rc$Now_hourly"
#rename rc to the new name
getRename="git branch -m $newrcName"
#Delete the old-name remote branch
deleteOldBranchRemote="git push origin --delete rc"
${getRename}
#if getRename has error then do not execute the following line
#if [ $noErrorSomehowIneedToCheckForErrors ]
  #then
    ${deleteOldBranchRemote}
#fi
庭院

您可以这样写:

if git branch -m $newrcName; then
    git push origin --delete rc
fi

因此,仅当第一个命令以退出代码0(表示成功)结束时才执行第二个命令。

您可以通过运行来获取有关if关键字的更多信息help if我的系统的输出示例(Bash 4.3.46(1)-发行版):

if: if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi
Execute commands based on conditional.

The `if COMMANDS' list is executed.  If its exit status is zero, then the
`then COMMANDS' list is executed.  Otherwise, each `elif COMMANDS' list is
executed in turn, and if its exit status is zero, the corresponding
`then COMMANDS' list is executed and the if command completes.  Otherwise,
the `else COMMANDS' list is executed, if present.  The exit status of the
entire construct is the exit status of the last command executed, or zero
if no condition tested true.

Exit Status:
Returns the status of the last command executed.

如果您想知道错误代码,可以从$?读取它。Bash将最后执行的命令的退出代码存储在此变量中。您可以将其存储在变量中以供以后使用:

git branch -m $newrcName
BRANCH_EXIT_CODE=$?
echo "git branch -m $newrcName exit code was $BRANCH_EXIT_CODE"
# $? now contains the exit code of the preceding echo
if [ $BRANCH_EXIT_CODE -eq 0 ]; then
    git push origin --delete rc
fi

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Bash脚本错误检查

来自分类Dev

Bash脚本错误检查

来自分类Dev

检查用户输入中bash脚本中的错误

来自分类Dev

如何在bash脚本中检查apt-get错误?

来自分类Dev

如何在bash脚本中检查apt-get错误?

来自分类Dev

bash脚本中的替换错误

来自分类Dev

bash脚本中的逻辑错误

来自分类Dev

在bash脚本中检查BSUB的语法

来自分类Dev

Bash脚本-检查参数-中的文件

来自分类Dev

OpenSSL脚本在bash中运行检查

来自分类Dev

在Bash脚本中检查命令的结果

来自分类Dev

在bash脚本中检查无效日期

来自分类Dev

如何检查bash脚本中的docker或主机?

来自分类Dev

检查 bash 脚本中是否存在变量

来自分类Dev

bash脚本中的“错误替换”错误

来自分类Dev

bash 脚本中的错误:算术错误

来自分类Dev

检查脚本命令中的错误

来自分类Dev

如何使用-e检查bash中的错误

来自分类Dev

在bash脚本中捕获mysqldump错误

来自分类Dev

bash脚本中的pip -r错误

来自分类Dev

bash脚本中的参数过多错误

来自分类Dev

如何修复bash脚本中的数组错误?

来自分类Dev

bash脚本中的返回值错误

来自分类Dev

Ubuntu 14.04中的bash脚本错误

来自分类Dev

简单的bash脚本中的错误-MV + AWK

来自分类Dev

bash脚本中的颜色错误/警告/信息

来自分类Dev

错误:bash shell脚本中的while循环

来自分类Dev

在 bash 脚本中获取 curl 错误描述?

来自分类Dev

Bash脚本检查差异