Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash shell, trying to create and evaluate a mask

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

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" >_>

like image 321
Neuromante Avatar asked Dec 05 '13 10:12

Neuromante


People also ask

What does [- Z $1 mean in bash?

$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.

What does ${} mean in shell?

$() – the command substitution. ${} – the parameter substitution/variable expansion.

What does #$ mean in bash?

#$ does "nothing", as # is starting comment and everything behind it on the same line is ignored (with the notable exception of the "shebang"). $# prints the number of arguments passed to a shell script (like $* prints all arguments).

What is $1 and $2 in shell script?

These are positional arguments of the script. Executing ./script.sh Hello World. Will make $0 = ./script.sh $1 = Hello $2 = World. Note. If you execute ./script.sh , $0 will give output ./script.sh but if you execute it with bash script.sh it will give output script.sh .


1 Answers

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

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

mosk=$(($mask & $mesk))
like image 125
Some programmer dude Avatar answered Sep 24 '22 13:09

Some programmer dude