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
  • Simple if else Script
  • Simple if, elif, else Script
  • Operators

Was this helpful?

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

if, else, and elif

We can write scripts that can perform certain actions depending on conditions using if, else and elif. The syntax for these things is quite particular so you need to be careful. At a high level, we are looking at...

High Level Psuedo Code - if statement
if [Some form of conditional test]
then
    do thing A
else
    do thing B
fi

Simple if else Script

The easiest way to show this in action is to examine a working script with an if statement in it:

#!/bin/bash

echo "How old are you?"

read age

if [ $age -lt 18 ]
then
  echo "I think you are too young for this game"
else
  echo "Welcome to the club"
fi

Simple if, elif, else Script

The above example only allows for 2 execution branches; one if a condition is true, or the other if not. This is fine if your script is fairly simple, however this can be rather limiting. So, we can add the elif statement to add further conditional checks, providing more granularity to the tool:

High Level Psuedo Code - if else statement
if [ <some test> ]
then
  <perform an action>
elif
then
  <perform this action instead>
else
  <perform yet another different action>
fi

Again, let's examine a working example:

#!/bin/bash

echo "How old are you?"

read age

if [ $age -lt 18 ]
then
  echo "I think you are too young for this game"
elif [ $age -gt 60 ]
then
  echo "Well, arent you the old codger"
else
  echo "Welcome to the club"
fi

In both scripts, you are seeing operators such as -lt and -gt, which are short for Less Than and Greater Than. The table below contains a comprehensive list of operators you can use in your scripts

Operators

Operator

Description

!EXPRESSION

The EXPRESSION is false

-n STRING

STRING length is greater than zero

-z STRING

The length of STRING is zero (empty)

STRING1 = STRING 2

STRING1 is equal to STRING2

STRING1 != STRING 2

STRING1 is not equal to STRING2

INTEGER1 -eq INTEGER2

INTEGER1 is equal to INTEGER2

INTEGER1 -ne INTEGER2

INTEGER1 is not equal to INTEGER2

INTEGER1 -gt INTEGER2

INTEGER1 is greater then INTEGER2

INTEGER1 -lt INTEGER2

INTEGER1 is less than INTEGER2

INTEGER1 -ge INTEGER2

INTEGER1 is greater than or equal to INTEGER2

INTEGER1 -le INTEGER2

INTEGER1 is less than or equal to INTEGER2

-d FILE

FILE exists and is a Directory

-e FILE

FILE exists

-r FILE

FILE exists and has read permission

-s FILE

FILE exists and is not empty

-w FILE

FILE exists and has write permission

-x FILE

FILE exists and has execute permission

PreviousReading User InputNextBOOLEAN Logic

Last updated 1 year ago

Was this helpful?