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?
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)
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.
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.
Sometimes two conditions can be true at the same time.
Sometimes, instead of checking if something does exist, you want to make sure it doesn't exist. This is called "negation".
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.
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: