Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract bits from return code number in 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!

like image 492
lzap Avatar asked Jul 08 '11 15:07

lzap


2 Answers

in Bash you can use double parenthesis:

#fatal error
errorcode=7
(( res = errorcode & 3 ))
[[ $res != 0 ]] && echo "Fatal Error"
like image 86
neurino Avatar answered Nov 02 '22 22:11

neurino


Bash supports bitwise operators...

$ let "x = 5>>1"
$ echo $x
2
$ let "x = 5 & 4"
$ echo $x
4
like image 28
Andrew White Avatar answered Nov 02 '22 22:11

Andrew White