从 bash 脚本访问 ERRORLEVEL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6827837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:47:56  来源:igfitidea点击:

accessing ERRORLEVEL from bash script

bashcmderrorlevel

提问by shipshape

I have an application that only works properly when called from a windows command prompt. Something to do with the input/output streams.

我有一个应用程序,只有在从 Windows 命令提示符调用时才能正常工作。与输入/输出流有关。

So I can call it from a bash script by passing it as an argument to cmd.

因此,我可以通过将其作为参数传递给 cmd 来从 bash 脚本中调用它。

cmd /c "badapp"

cmd /c "badapp"

This works fine - but occasionally badapp fails with network problems - and I get no feedback. Is there anyway to check the ERRORLEVEl from the bash script - or see the output from badapp on the terminal running the bash script?

这工作正常 - 但偶尔 badapp 因网络问题而失败 - 我没有得到任何反馈。无论如何要检查 bash 脚本中的 ERRORLEVEl - 或者在运行 bash 脚本的终端上查看 badapp 的输出?

回答by Susam Pal

yes, $?is the variable that contains the error level.

是的,$?是包含错误级别的变量。

Try echo $?for example.

echo $?例如尝试。

An example from Cygwin bash (I'm guessing you are using Cygwin because you are using the Windows cmdin your example.)

Cygwin bash 的一个示例(我猜您正在使用 Cygwin,因为您cmd在示例中使用的是 Windows 。)

susam@nifty /cygdrive/c/Documents and Settings/susam/Desktop
$ cmd /c "badapp"
'badapp' is not recognized as an internal or external command,
operable program or batch file.

susam@nifty/cygdrive/c/Documents and Settings/susam/Desktop
$ if [ $? -eq 0 ]
> then
>   echo "good"
> else
>   echo "bad"
> fi
bad