如何从 Bash 中的返回码中提取位
我正在使用返回此错误代码的 pylint 实用程序:
Pylint should leave with following status code:
* 0 if everything went fine
* 1 if a fatal message was issued
* 2 if an error message was issued
* 4 if a warning message was issued
* 8 if a refactor message was issued
* 16 if a convention message was issued
* 32 on usage error
status 1 to 16 will be bit-ORed so you can know which different
categories has been issued by analysing pylint output status code
现在我需要确定 Bash 中是否出现致命或错误消息。怎么做呢?我想我需要位操作;-)
编辑:我知道我需要按位和数字三 (3) 进行操作,并针对 null 进行测试,以查看是否发出了致命消息或错误消息。我的问题很简单:使用 bash 语法来实现。输入是 $?,输出又是 $? (例如使用测试程序)。谢谢!
I am using pylint utility that returns this error codes:
Pylint should leave with following status code:
* 0 if everything went fine
* 1 if a fatal message was issued
* 2 if an error message was issued
* 4 if a warning message was issued
* 8 if a refactor message was issued
* 16 if a convention message was issued
* 32 on usage error
status 1 to 16 will be bit-ORed so you can know which different
categories has been issued by analysing pylint output status code
Now I need to determine if fatal or error message occured in Bash. How to do that? I guess I need bit operations for that ;-)
Edit: I know I need to do bitwise and with number three (3) and test against null to see if fatal message or error message were issued. My problem is simple: bash syntax to do it. Input is $?, ouptut is again $? (e.g. using test program). Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
当且仅当状态为奇数且最低有效数字为 1 时,将发出致命消息。
当且仅当状态的下一个最高有效数字为 1 时,将发出错误消息。
所以你想检查最后两位是否都是1;换句话说,检查
0b11
状态代码的按位and
是否为 3。A fatal message will be issued iff the status is odd, iff it has a 1 in the least significant digit.
An error message will be issued iff the status has a 1 in the next most significant digit.
So you want to check whether the last two digits are both 1; in other words, to check whether the bitwise
and
of your status code with0b11
is three.要将类似的内容嵌入到设置了
errexit
的脚本中,您可以使用如下形式:灵感来自 David 的评论 和 这个答案
你可以通过用
python -c 替换
pylint blah blah
来查看它退出(4+8+16)”To embed something like this in a script with
errexit
set, you could use a form like the following:Inspired from David's comment and this answer
You could poke at it by replacing the
pylint blah blah
withpython -c "exit(4+8+16)"
bash 中最后执行的命令的返回码可用作
$?
。如果您要从 python 的
subprocess
模块调用外部命令,一旦子进程退出,您就可以从 Popen 对象获取外部命令的返回码。The return code of the last executed command in bash is available as
$?
.If you were to call an external command from say python's
subprocess
module, you could get the return code of the external command from the Popen object once the subprocess has exited.使用(可能不是最佳的)bash 算术内容:
输出:
我强烈怀疑脚本(测试)可以变得更紧凑或更清晰(特别是在 elif 子句中),但这似乎有效(我需要去工作)。
Using (probably sub-optimally) the bash arithmetic stuff:
Output:
I strongly suspect that the scripting (testing) can be made tighter or cleaner (specifically in the
elif
clause), but this seems to work (and I need to get to work).在 Bash 中你可以使用双括号:
in Bash you can use double parenthesis:
知道了!
谢谢!
Got it!
Thanks!
Bash 支持按位运算符...
Bash supports bitwise operators...