HB Computer Security | Aide Memoire
  • Introduction
  • Core Technical Skills
    • Core Skills
      • Linux
        • Getting to Know Linux
          • Using the Shell
            • Shells, Terminals, and Virtual Consoles
            • Choose your Shell
            • Shell Commands
              • Non-PATH Commands
              • Command History
                • Command Line Editing
                  • Keystrokes for Navigating Command Lines
                  • Keystrokes for Editing Command Lines
                  • Keystrokes for Cutting and Pasting Text from within Command Lines
                • Command Line Recall
                  • Keystrokes for Command Line Recall
              • Connecting and Expanding Commands
                • Piping Between Commands
                • Sequential Commands
                • Expanding Commands
            • Shell Variables
              • Common Shell Variables
            • Aliases
            • Create your Own Shell Environment
              • Modification Ideas
          • Navigating the Linux File System (LFS)
            • Filesystem Commands
            • Listing Files and Directories
            • File Permissions and Ownership
              • Modifying Permissions with chmod
              • Modifying Default Permissions with umask
              • Change File Ownership with chown
            • Copying, Moving, and Removing Files
            • Finding Files
              • locate
              • find
              • grep
            • Downloading Files
              • axel
              • wget
              • curl
                • User-Agent: Googlebot
          • Working with Text Files
            • Using vim and vi to Edit Text Files
              • Starting with vi
              • Adding Text
              • Moving Around in the Text
            • Text Manipulation
        • System Administration
          • Installing Linux
            • Installing from Live Media
            • Installing in the Enterprise
            • Partitioning Hard Disks
              • Tips for Creating Partitions
          • Account Administration
            • The root Account
              • Becoming root with su
              • sudo
                • The /etc/sudoers File
                • Granting sudo privileges
                  • visudo Guidance
                • Useful sudo Hints
            • Other Administrative Accounts
            • Standard User Accounts
              • Risks of userdel: Orphaned Files
          • Graphical Remote Administration
            • Cockpit
              • Installation Guide
            • Remote Desktop Protocol with xrdp
              • Installation and Configuration
            • Remote Desktop with vnc
              • Installation and Configuration
              • Running VNC as a System Service
          • Managing Running Processes
            • Listing Processes
              • ps
              • top
              • htop
            • Backgrounding and Foregrounding
              • Starting a Background Process
              • Using Foreground and Background Commands
            • Killing and Recining Processes
              • kill and killall
          • Managing Software
            • Managing Software from the Desktop
            • Going Beyond the Limitations of Software Center
              • Debian Packages
                • Advanced Package Tool (apt)
                • Repositories
                • dpkg
        • Shell Scripting
          • Variables
            • Command Substitution
            • Arguments
          • Reading User Input
          • if, else, and elif
          • BOOLEAN Logic
          • Loops
            • for Loops
            • while Loops
          • Functions
          • Local Vs Global Variables
          • Summary
        • Securing Linux
      • Windows
        • Security Hardening
Powered by GitBook
On this page

Was this helpful?

  1. Core Technical Skills
  2. Core Skills
  3. Linux
  4. Shell Scripting

BOOLEAN Logic

Previousif, else, and elifNextLoops

Last updated 1 year ago

Was this helpful?

Boolean logical operators like AND (&&), and OR (||) are somewhat mysterious because Bash uses them in a variety of ways. Putting it as simply as is possible, BOOLEAN logic looks for a True or False scenario, and depending on which operator you use, a certain action will be performed. So, with AND, some initial condition is tested and if it is found to be True, it will then perform the second - BOTH elements have to be found True for successful execution. With OR, the same conditional test will take place, but either one or the other side must be found True, not both. The examples below demonstrate this, and they are fairly simple, so once you've seen them things become quite clear.

A common use is in command lists, which are chains of commands whose flow is controlled by operators:

BOOLEAN operator example - AND
user2=ubuntu
grep $user2 /etc/passwd && echo "$user2 found!"

So, what happened here then? A variable was declared, holding the value of a valid user name. Then, the grep command was used to search the /etc/passwd file for an entry that matched the username; in this case, ubuntu. So, the grep command completes, and there is indeed a match, there was a matching user accounts with the name ubuntu, meaning that the BOOLEAN True/False test was positive.

In this situation, the AND result must be true for the next command to be executed, else execution will cease. On this occasion execution continues, and the second command (echo) is executed, printing our message to the terminal (STDOUT).

If we change the value of user2 to a user not present in the system, and run the same BOOLEAN command, you will notice that the output is different:

On this occasion, the AND condition fails as the username doesnotexist cannot be found in the /etc/passwd file, hence the second command is never executed.

If we were to modify the command, replacing AND with OR, we would see our desired echo message, despite the username not appearing in the /etc/passwd file:

As you have doubtlessly worked out, this is because with the OR statement, either condition needs to be True, not both, and because the second condition is merely an echo command, it was always going to be true irrespective of the initial conditional check.

Whilst the above examples are very simple, the power of BOOLEAN operators means that your scripts can contain some quite powerful decision making calls, depending on certain conditions being made

Example of BOOLEAN AND
BOOLEAN AND with a false outcome
BOOLEAN OR with successful execution from a FALSE conditon