Bash programming 101 - logic

dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
3,678
Reaction score
3,498
Credits
32,826
I highly recommend you read the article of bash variables first.

Logic, what is logic? Well in bash scripting it usually asks the question of whether something is true of not. If this is true, do this. If it isn't true, do that. So what do some examples of this look like?

Code:
#!/bin/bash

num1=10
num2=20

if [ $num1 -gt $num2 ]; then
  echo "$num1 is greater than $num2"
elif [ $num1 -lt $num2 ]; then
  echo "$num1 is less than $num2"
else
  echo "$num1 is equal to $num2"
fi

Here we have two numbers. The questions is asked... "if" something is true, echo this. Or else "elif" echo that. If neither is true "else"
then echo something else. I used echo in this, but you can run almost command based on whether something is true of not.

The basic keyword is bash shell scripting for login are...
Greater than (-gt)
Less than (-lt)
Equal to (-eq)

To check of a file or directory exists...
File exists (-e)
Directory exists (-d)


Code:
#!/bin/bash

file="example.txt"

if [ -e "$file" ]; then
  echo "File $file exists."
else
  echo "File $file does not exist."
fi

This just checks to see if a file named example.txt exixts in the current directory. You could replace the -e, with a -d to see if a directory named "example_dir" existed.

Code:
#!/bin/bash

directory="example_dir"

if [ -d "$directory" ]; then
  echo "Directory $directory exists."
else
  echo "Directory $directory does not exist."
fi

Greater than, and less than are great for numbers. But not so great for words and strings of text. We use a different kind of logic test for those.
String Comparison: = for equal, != for not equal.
Logical AND (&&) and OR (||): Combine multiple conditions.
Negation (!): Negate a condition.

Code:
#!/bin/bash

str1="hello"
str2="world"

if [ "$str1" = "$str2" ]; then
  echo "Strings are equal."
else
  echo "Strings are not equal."
fi

Sometimes two conditions can be true at the same time.

Code:
#!/bin/bash

num1=10
num2=20

if [ $num1 -gt 5 ] && [ $num2 -lt 30 ]; then
  echo "Both conditions are true."
fi

if [ $num1 -gt 15 ] || [ $num2 -lt 30 ]; then
  echo "At least one condition is true."
fi

Sometimes, instead of checking if something does exist, you want to make sure it doesn't exist. This is called "negation".

Code:
#!/bin/bash

file="example.txt"

if [ ! -e "$file" ]; then
  echo "File $file does not exist."
fi

Note that "if" conditions, always end with a "fi" (finish). If your script has multiple "if" statements, it should have an equal number of "fi" statements.
 
Last edited:


I have some minor errata for you:
With bash’s test command (which is also aliased as [), the -e flag only checks that a variable contains a path to a file-system object that exists in the file-system. It doesn’t check what type of object it is. It could be a file, or a directory, or a symbolic link, or a named pipe etc etc.

If you want to ensure a variable contains a valid path to a file you should use:
Bash:
if [ -f "$file" ]; then
  echo "$file is a file"
else
  echo "$file IS NOT a file"
fi

There are switches to allow you to test for other specific types of file system objects. You can also check whether variables are initialised or empty, check permissions on a filesystem object.
E.g. does the user have write access to a filesystem object, or is the object executable for the user etc etc.
See man test for more details, or view the link below:
 


Members online


Top