![]() |
|
Logical & in Bash - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Computers (https://sickgaming.net/forum-86.html) +--- Forum: Linux, BSD & Unix (https://sickgaming.net/forum-88.html) +--- Thread: Logical & in Bash (/thread-88792.html) |
Logical & in Bash - xSicKxBot - 02-19-2019 Logical & in Bash One would think you could dispatch Code: &Code: &Code: &Code: <Code: >This means we haven’t even touched on Code: && is a Bitwise Operator 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: &Code: |AND Code: 0 & 0 = 0 0 & 1 = 0 1 & 0 = 0 1 & 1 = 1OR Code: 0 | 0 = 0 0 | 1 = 1 1 | 0 = 1 1 | 1 = 1You can test this by ANDing any two numbers and outputting the result with Code: echoCode: $ echo $(( 2 & 3 )) # 00000010 AND 00000011 = 00000010 2 $ echo $(( 120 & 97 )) # 01111000 AND 01100001 = 01100000 96The same goes for OR ( Code: |Code: $ echo $(( 2 | 3 )) # 00000010 OR 00000011 = 00000011 3 $ echo $(( 120 | 97 )) # 01111000 OR 01100001 = 01111001 121Three things about this:
TIP: If you want to check what your decimal number would look like in binary, you can use bc, the command-line calculator that comes preinstalled with most Linux distros. For example, using: Code: bc <<< "obase=2; 97"will convert Code: 97Code: obaseCode: bc <<< "ibase=2; 11001011"will convert Code: 11001011Code: ibase&& is a Logical Operator Although it uses the same logic principles as its bitwise cousin, Bash’s Code: &&Code: $ echo $(( 4 && 5 )) # Both non-zero numbers, both true = true 1 $ echo $(( 0 && 5 )) # One zero number, one is false = false 0 $ echo $(( b && 5 )) # One of them is not number, one is false = false 0The OR counterpart for Code: &&Code: ||All of this is simple enough… until it comes to a command’s exit status. && is a Logical Operator for Command Exit Status As we have seen in previous articles, 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 exit code, 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. 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 the exact contrary of what you saw in the logical operations above, but what are you gonna do? Different contexts, different rules. The usefulness of this will become apparent soon enough. Moving on. Exit codes are stored temporarily in the special variable Code: ?Code: $Code: ?Try it with: Code: $ find /etc -iname "*.service" find: '/etc/audisp/plugins.d': Permission denied /etc/systemd/system/dbus-org.freedesktop.nm-dispatcher.service /etc/systemd/system/dbus-org.freedesktop.ModemManager1.service [etcetera]As you saw in the previous article, running Code: findSo, if you execute… Code: echo $?… right after Code: findCode: 1(Notice that if you were to run Code: echo $?Code: $?Code: echo $?Code: $?Code: $?One immediate use of Code: ?Code: $ configure . . . $ make . . . $ make install . . .You can also put all three on one line… Code: $ configure; make; make install… and hope for the best. The disadvantage of this is that if, say, Code: configureCode: makeCode: sudo make installThe smarter way of doing it is like this: Code: $ configure && make && make installThis takes the exit code from each command and uses it as an operand in a chained Code: &&But, and here’s the kicker, Bash knows the whole thing is going to fail if Code: configureCode: makeCode: makeCode: configure && makeCode: make installCode: &&You can do something similar with Code: ||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 this article does: Code: mkdir test_dir 2>/dev/null || touch backup/dir/images.txt && find . -iname "*jpg" > backup/dir/images.txt &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! |