如何从 Bash 中的返回码中提取位

发布于 2024-11-18 21:30:31 字数 672 浏览 4 评论 0原文

我正在使用返回此错误代码的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

荆棘i 2024-11-25 21:30:32

当且仅当状态为奇数且最低有效数字为 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 with 0b11 is three.

但可醉心 2024-11-25 21:30:32

要将类似的内容嵌入到设置了 errexit 的脚本中,您可以使用如下形式:

#!/bin/bash
set -o errexit
set -o nounset
(
    rc=0;
    pylint args args || rc=$?;
    exit $(( $rc & 35 )) # fatal=1 | error=2 | usage error=32
)

灵感来自 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:

#!/bin/bash
set -o errexit
set -o nounset
(
    rc=0;
    pylint args args || rc=$?;
    exit $(( $rc & 35 )) # fatal=1 | error=2 | usage error=32
)

Inspired from David's comment and this answer

You could poke at it by replacing the pylint blah blah with python -c "exit(4+8+16)"

草莓酥 2024-11-25 21:30:32

bash 中最后执行的命令的返回码可用作 $?

[/tmp] % touch bar
[/tmp] % ls /tmp/bar 
/tmp/bar
[/tmp] % echo $?
0
[/tmp] % ls /tmp/baaz
ls: /tmp/baaz: No such file or directory
[/tmp] % echo $?
1
[/tmp] % 

如果您要从 python 的 subprocess 模块调用外部命令,一旦子进程退出,您就可以从 Popen 对象获取外部命令的返回码。

The return code of the last executed command in bash is available as $?.

[/tmp] % touch bar
[/tmp] % ls /tmp/bar 
/tmp/bar
[/tmp] % echo $?
0
[/tmp] % ls /tmp/baaz
ls: /tmp/baaz: No such file or directory
[/tmp] % echo $?
1
[/tmp] % 

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.

淡紫姑娘! 2024-11-25 21:30:32

使用(可能不是最佳的)bash 算术内容:

for status in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
do
    if [ $status = 0 ]
    then echo $status: it worked perfectly
    elsif [ $(( $status & 3 )) != 0 ]
    then echo $status: a fatal or error message was sent
    else echo $status: it sort of worked mostly
    fi
done

输出:

0: it worked perfectly
1: a fatal or error message was sent
2: a fatal or error message was sent
3: a fatal or error message was sent
4: it sort of worked mostly
5: a fatal or error message was sent
6: a fatal or error message was sent
7: a fatal or error message was sent
8: it sort of worked mostly
9: a fatal or error message was sent
10: a fatal or error message was sent
11: a fatal or error message was sent
12: it sort of worked mostly
13: a fatal or error message was sent
14: a fatal or error message was sent
15: a fatal or error message was sent
16: it sort of worked mostly

我强烈怀疑脚本(测试)可以变得更紧凑或更清晰(特别是在 elif 子句中),但这似乎有效(我需要去工作)。

pylint ...
status=$?     # Catch exit status before it changes
if [ $status = 0 ]
then echo $status: it worked perfectly
elsif [ $(( $status & 3 )) != 0 ]
then echo $status: a fatal or error message was sent
else echo $status: it sort of worked mostly
fi

Using (probably sub-optimally) the bash arithmetic stuff:

for status in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
do
    if [ $status = 0 ]
    then echo $status: it worked perfectly
    elsif [ $(( $status & 3 )) != 0 ]
    then echo $status: a fatal or error message was sent
    else echo $status: it sort of worked mostly
    fi
done

Output:

0: it worked perfectly
1: a fatal or error message was sent
2: a fatal or error message was sent
3: a fatal or error message was sent
4: it sort of worked mostly
5: a fatal or error message was sent
6: a fatal or error message was sent
7: a fatal or error message was sent
8: it sort of worked mostly
9: a fatal or error message was sent
10: a fatal or error message was sent
11: a fatal or error message was sent
12: it sort of worked mostly
13: a fatal or error message was sent
14: a fatal or error message was sent
15: a fatal or error message was sent
16: it sort of worked mostly

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).

pylint ...
status=$?     # Catch exit status before it changes
if [ $status = 0 ]
then echo $status: it worked perfectly
elsif [ $(( $status & 3 )) != 0 ]
then echo $status: a fatal or error message was sent
else echo $status: it sort of worked mostly
fi
傲影 2024-11-25 21:30:31

在 Bash 中你可以使用双括号:

#fatal error
errorcode=7
(( res = errorcode & 3 ))
[[ $res != 0 ]] && echo "Fatal Error"

in Bash you can use double parenthesis:

#fatal error
errorcode=7
(( res = errorcode & 3 ))
[[ $res != 0 ]] && echo "Fatal Error"
眼泪也成诗 2024-11-25 21:30:31

知道了!

[ $(($NUMBER & 3)) -ne 0 ] && echo Fatal error or error was issued

谢谢!

Got it!

[ $(($NUMBER & 3)) -ne 0 ] && echo Fatal error or error was issued

Thanks!

辞取 2024-11-25 21:30:31

Bash 支持按位运算符...

$ let "x = 5>>1"
$ echo $x
2
$ let "x = 5 & 4"
$ echo $x
4

Bash supports bitwise operators...

$ let "x = 5>>1"
$ echo $x
2
$ let "x = 5 & 4"
$ echo $x
4
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文