Bash shell,尝试创建和评估掩码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20397692/
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-18 08:53:29  来源:igfitidea点击:

Bash shell, trying to create and evaluate a mask

bashshellbit-manipulationmaskbitmask

提问by Neuromante

I'm trying to create a mask and use the bitwise operator "&" to compare to another variable and see the output. Let there be code:

我正在尝试创建一个掩码并使用按位运算符“&”与另一个变量进行比较并查看输出。让有代码:

mask=00000
mesk=00010
mosk=$mask&$mesk
echo $mosk
echo meec

I'm trying to expand this functionality to be able to have more characters (different error/success codes), but those lines just don't work: Executing the script will print an empty line, then "meec".

我正在尝试扩展此功能以包含更多字符(不同的错误/成功代码),但这些行不起作用:执行脚本将打印一个空行,然后是“meec”。

I came from an object oriented programming background, and although I've read through several documents on this subject, it seems there's something I'm missing. Any help would be appreciated.

我来自面向对象的编程背景,虽然我已经阅读了关于这个主题的几个文档,但似乎我遗漏了一些东西。任何帮助,将不胜感激。

Edit: For some reason, turns out the code doesn't work, it says "command 00010 not found" >_>

编辑:由于某种原因,代码不起作用,它说“找不到命令 00010”>_>

采纳答案by Some programmer dude

It's because usually the &character in the shell is the modifier to put a command in the background.

这是因为通常&shell 中的字符是将命令置于后台的修饰符。

You have to use Arithmetic Expansionof Bash (for example) for it to work:

您必须使用Bash 的算术扩展(例如)才能使其工作:

mosk=$(($mask & $mesk))