如何捕捉bash中的水银错误?

问题描述

| 如何在bash中获取
hg update
命令的结果并使用结果? 我对true / false值或类似的值感兴趣。我感兴趣的错误之一是未知版本。

解决方法

首先,我在子外壳中运行它并捕获输出和退出代码
result=$(hg update 2>&1)
exit_code=$?
然后
case $exit_code in
0)
  success
  ;;
[1-5])
  failure x
  ;;
[6-9])
  failure y
  ;;
255)
  failure z
  ;;
*) # Default
  echo \"it\'s a trap\"
  ;;
esac
或者,如果您对真/假状态运行感兴趣,则可以
result=$(hg update 2>&1) && echo \"Success\"
要么
result=$(hg update 2>&1) || echo \"Failure\"
,
$?
给您您想要的吗?运行
hg update
命令后,尝试\“
echo $?
\”。通常,零表示“确定”,非零表示出了问题。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...