Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Logical & in Bash

#1
Logical & in Bash

<div style="margin: 5px 5% 10px 5%;"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/02/logical-in-bash.jpg" width="1624" height="757" title="" alt="" /></div><div><div><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/02/logical-in-bash.jpg" class="ff-og-image-inserted" /></div>
<p>One would think you could dispatch <code>&amp;</code> in two articles. Turns out you can’t. While <a href="https://www.linux.com/blog/learn/2019/2/and-ampersand-and-linux">the first article dealt with using <code>&amp;</code> at the end of commands to push them into the background</a> and then diverged into explaining process management, the second article saw <a href="https://www.linux.com/blog/learn/2019/2/ampersands-and-file-descriptors-bash"><code>&amp;</code> being used as a way to refer to file descriptors</a>, which led us to seeing how, combined with <code>&lt;</code> and <code>&gt;</code>, you can route inputs and outputs from and to different places.</p>
<p>This means we haven’t even touched on <code>&amp;</code> as an AND operator, so let’s do that now.</p>
<h3>&amp; is a Bitwise Operator</h3>
<p>If you are at all familiar with binary operations, you will have heard of AND and OR. These are bitwise operations that operate on individual bits of a binary number. In Bash, you use <code>&amp;</code> as the AND operator and <code>|</code> as the OR operator:</p>
<p><strong>AND</strong></p>
<pre>
0 &amp; 0 = 0 0 &amp; 1 = 0 1 &amp; 0 = 0 1 &amp; 1 = 1
</pre>
<p><strong>OR</strong></p>
<pre>
0 | 0 = 0 0 | 1 = 1 1 | 0 = 1 1 | 1 = 1
</pre>
<p>You can test this by ANDing any two numbers and outputting the result with <code>echo</code>:</p>
<pre>
$ <b>echo $(( 2 &amp; 3 ))</b> # 00000010 AND 00000011 = 00000010 2 $ <b>echo $(( 120 &amp; 97 ))</b> # 01111000 AND 01100001 = 01100000 96 </pre>
<p>The same goes for OR (<code>|</code>):</p>
<pre> $ <b>echo $(( 2 | 3 ))</b> # 00000010 OR 00000011 = 00000011 3 $ <b>echo $(( 120 | 97 ))</b> # 01111000 OR 01100001 = 01111001 121
</pre>
<p>Three things about this:</p>
<ol>
<li>You use <code>(( ... ))</code> to tell Bash that what goes between the double brackets is some sort of arithmetic or logical operation. <code>(( 2 + 2 ))</code>, <code>(( 5 % 2 ))</code> (<code>%</code> being the <a href="https://en.wikipedia.org/wiki/Modulo_operation">modulo</a> operator) and <code>((( 5 % 2 ) + 1))</code> (equals 3) will all work.</li>
<li><a href="https://www.linux.com/blog/learn/2018/12/bash-variables-environmental-and-otherwise">Like with variables</a>, <code>$</code> extracts the value so you can use it.</li>
<li>For once spaces don’t matter: <code>((2+3))</code> will work the same as <code>(( 2+3 ))</code> and <code>(( 2 + 3 ))</code>.</li>
<li>Bash only operates with integers. Trying to do something like this <code>(( 5 / 2 ))</code> will give you “2”, and trying to do something like this <code>(( 2.5 &amp; 7 ))</code> will result in an error. Then again, using anything but integers in a bitwise operation (which is what we are talking about now) is generally something you wouldn’t do anyway.</li>
</ol>
<p><b>TIP:</b> If you want to check what your decimal number would look like in binary, you can use <i>bc</i>, the command-line calculator that comes preinstalled with most Linux distros. For example, using:</p>
<pre> bc &lt;&lt;&lt; "obase=2; 97" </pre>
<p>will convert <code>97</code> to binary (the <i>o</i> in <code>obase</code> stands for <i>output</i>), and …</p>
<pre> bc &lt;&lt;&lt; "ibase=2; 11001011" </pre>
<p>will convert <code>11001011</code> to decimal (the <i>i</i> in <code>ibase</code> stands for <i>input</i>).</p>
<h3>&amp;&amp; is a Logical Operator</h3>
<p>Although it uses the same logic principles as its bitwise cousin, Bash’s <code>&amp;&amp;</code> operator can only render two results: 1 (“true”) and 0 (“false”). For Bash, any number not 0 is “true” and anything that equals 0 is “false.” What is also false is anything that is not a number:</p>
<pre> $ <b>echo $(( 4 &amp;&amp; 5 ))</b> # Both non-zero numbers, both true = true 1 $ <b>echo $(( 0 &amp;&amp; 5 ))</b> # One zero number, one is false = false 0 $ <b>echo $(( b &amp;&amp; 5 ))</b> # One of them is not number, one is false = false 0
</pre>
<p>The OR counterpart for <code>&amp;&amp;</code> is <code>||</code> and works exactly as you would expect.</p>
<p>All of this is simple enough… until it comes to a command’s exit status.</p>
<h3>&amp;&amp; is a Logical Operator for Command Exit Status</h3>
<p><a href="https://www.linux.com/blog/learn/2019/2/ampersands-and-file-descriptors-bash">As we have seen in previous articles</a>, as a command runs, it outputs error messages. But, more importantly for today’s discussion, it also outputs a number when it ends. This number is called an <i>exit code</i>, and if it is 0, it means the command did not encounter any problem during its execution. If it is any other number, it means something, somewhere, went wrong, even if the command completed.</p>
<p>So 0 is good, any other number is bad, and, in the context of exit codes, 0/good means “true” and everything else means “false.” Yes, this is <b>the exact contrary of what you saw in the logical operations above</b>, but what are you gonna do? Different contexts, different rules. The usefulness of this will become apparent soon enough.</p>
<p>Moving on.</p>
<p>Exit codes are stored <i>temporarily</i> in the <a href="https://www.gnu.org/software/bash/manual/html_node/Special-Parameters.html">special variable</a> <code>?</code> — yes, I know: another confusing choice. Be that as it may, <a href="https://www.linux.com/blog/learn/2018/12/bash-variables-environmental-and-otherwise">remember that in our article about variables</a>, and we said that you read the value in a variable using a the <code>$</code> symbol. So, if you want to know if a command has run without a hitch, you have to read <code>?</code> as soon as the command finishes and before running anything else.</p>
<p>Try it with:</p>
<pre> $ <b>find /etc -iname "*.service"</b> find: '/etc/audisp/plugins.d': Permission denied /etc/systemd/system/dbus-org.freedesktop.nm-dispatcher.service /etc/systemd/system/dbus-org.freedesktop.ModemManager1.service <i>[etcetera]</i> </pre>
<p><a href="https://www.linux.com/blog/learn/2019/2/ampersands-and-file-descriptors-bash">As you saw in the previous article</a>, running <code>find</code> over <i>/etc</i> as a regular user will normally throw some errors when it tries to read subdirectories for which you do not have access rights.</p>
<p>So, if you execute…</p>
<pre> echo $? </pre>
<p>… right after <code>find</code>, it will print a <code>1</code>, indicating that there were some errors.</p>
<p>(Notice that if you were to run <code>echo $?</code> a second time in a row, you’d get a <code>0</code>. This is because <code>$?</code> would contain the exit code of <code>echo $?</code>, which, supposedly, will have executed correctly. So the first lesson when using <code>$?</code> is: <b>use <code>$?</code> straight away</b> or store it somewhere safe — like in another variable, or you will lose it).</p>
<p>One immediate use of <code>?</code> is to fold it into a list of chained commands and bork the whole thing if anything fails as Bash runs through it. For example, you may be familiar with the process of building and compiling the source code of an application. You can run them on after another by hand like this:</p>
<pre> $ <b>configure</b> . . . $ <b>make</b> . . . $ <b>make install</b> . . . </pre>
<p>You can also put all three on one line…</p>
<pre> $ <b>configure; make; make install</b> </pre>
<p>… and hope for the best.</p>
<p>The disadvantage of this is that if, say, <code>configure</code> fails, Bash will still try and run <code>make</code> and <code>sudo make install</code>, even if there is nothing to make or, indeed, install.</p>
<p>The smarter way of doing it is like this:</p>
<pre> $ <b>configure &amp;&amp; make &amp;&amp; make install</b>
</pre>
<p>This takes the exit code from each command and uses it as an operand in a chained <code>&amp;&amp;</code> operation.</p>
<p>But, and here’s the kicker, Bash knows the whole thing is going to fail if <code>configure</code> returns a non-zero result. If that happens, it doesn’t have to run <code>make</code> to check its exit code, since the result is going to be false no matter what. So, it forgoes <code>make</code> and just passes a non-zero result onto the next step of the operation. And, as <code>configure &amp;&amp; make</code> delivers false, Bash doesn’t have to run <code>make install</code> either. This means that, in a long chain of commands, you can join them with <code>&amp;&amp;</code>, and, as soon as one fails, you can save time as the rest of the commands get canceled immediately.</p>
<p>You can do something similar with <code>||</code>, the OR logical operator, and make Bash continue processing chained commands if only one of a pair completes.</p>
<p>In view of all this (along with the stuff we covered earlier), you should now have a clearer idea of what the command line we set at the beginning of <a href="https://www.linux.com/blog/learn/2019/2/and-ampersand-and-linux">this article does</a>:</p>
<pre>
mkdir test_dir 2&gt;/dev/null || touch backup/dir/images.txt &amp;&amp; find . -iname "*jpg" &gt; backup/dir/images.txt &amp;</pre>
<p>So, assuming you are running the above from a directory for which you have read and write privileges, what it does it do and how does it do it? How does it avoid unseemly and potentially execution-breaking errors? Next week, apart from giving you the solution, we’ll be dealing with brackets: curly, curvy and straight. Don’t miss it!</p>
</div>
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016