Make Your Shell Scripts Do More with Conditions and Control Structures: A Step-by-Step Guide
If-Else Statements
The most basic control structure in shell scripting is the if-else
statement. It allows you to test a condition and perform different actions based on whether the condition is true or false. The example for an if-else
statement is as follows:
#!/bin/bash
# Check if the number is greater than 5
number=10
if [ $number -gt 5 ]; then
echo "The number is greater than 5."
else
echo "The number is not greater than 5."
fi
Code Explanation:
In this example, we are testing the condition [ $number -gt 5 ]
to determine if the value of number
is greater than 5. If the condition is true, the script will output
"The number is greater than 5.". If the condition is false, the script
will output "The number is not greater than 5.".
If-Elif-Else Statements
The if-elif-else
statement allows you to test multiple conditions and perform different actions based on which condition is true. The example for an if-elif-else
statement is as follows:
#!/bin/bash
# Check the value of the number
number=10
if [ $number -lt 5 ]; then
echo "The number is less than 5."
elif [ $number -gt 5 ]; then
echo "The number is greater than 5."
else
echo "The number is equal to 5."
fi
Code Explanation:
In this example, we are testing two conditions: [ $number -lt 5 ] and [ $number -gt 5 ]. If the first condition is true, the script will output "The number is less than 5.". If the second condition is true, the script will output "The number is greater than 5.". If neither of the conditions is true, the script will output "The number is equal to 5.".
Case Statements
The case
statement is a control structure in shell scripting that allows you to test multiple values for a single variable and perform different actions based on which value is matched. The case
statement is often used when you need to test for multiple values for a single variable, and it can simplify your code and make it easier to read.
#!/bin/bash
# Check the value of the fruit
fruit=apple
case $fruit in
apple)
echo "The fruit is an apple."
;;
banana)
echo "The fruit is a banana."
;;
*)
echo "The fruit is not an apple or a banana."
;;
esac
Code Explanation:
In this example, we are testing the value of the fruit variable. If the value is "apple", the script will output "The fruit is an apple.". If the value is "banana", the script will output "The fruit is a banana.". If neither of the values is matched, the script will output "The fruit is not an apple or a banana.".
Using wildcards in Case Statement
You can also use wildcards in your case statements. For example, if you want to match multiple values, you can use a wildcard in the pattern:
#!/bin/bash
# Check the value of the fruit
fruit=orange
case $fruit in
apple|banana)
echo "The fruit is either an apple or a banana."
;;
*)
echo "The fruit is not an apple or a banana."
;;
esac
Code Explanation:
In this example, the script will output "The fruit is either an apple or a banana." if the value of the fruit variable is either "apple" or "banana".
The case statement is a powerful tool in shell scripting and can be used in many different situations where you need to test for multiple values for a single variable. Whether you are testing for values in a loop, checking the output of a command, or working with user input, the case statement can help you write efficient and effective shell scripts.
While Statements
The while
statement is a control structure in shell scripting that allows you to repeatedly execute a set of commands as long as a specified condition is met. The while
statement is often used when you need to perform a task repeatedly, such as processing data from a file, reading input from the user, or checking the status of a system.
Here's a simple example to demonstrate the use of a while
statement:
#!/bin/bash
# Keep printing the numbers 1 to 5
count=1
while [ $count -le 5 ]
do
echo $count
count=$((count + 1))
done
Code Explanation:
In this example, the while
statement tests the value of the count
variable. As long as count
is less than or equal to 5, the script will continue to execute the commands inside the loop and print the value of count
. When the value of count
becomes 6, the condition [ $count -le 5 ]
will evaluate to false, and the script will exit the loop.
The while
statement can also be used to read input from the user. For example:
#!/bin/bash
# Keep reading the input from the user until they enter "exit"
read -p "Enter your input: " input
while [ "$input" != "exit" ]
do
echo "You entered: $input"
read -p "Enter your input: " input
done
Code Explanation:
In this example, the script uses the read
command to read input from the user. The while
statement tests the value of the input
variable, and as long as the value is not "exit", the script will continue to execute the commands inside the loop and print the value of input
. When the user enters "exit", the condition [ "$input" != "exit" ]
will evaluate to false, and the script will exit the loop.
The while
statement is a fundamental control structure in shell scripting and is commonly used in many different scripts and scripts. Whether you are processing data, reading input from the user, or working with system processes, the while
statement can help you write efficient and effective shell scripts.
Nested If Statements
The if
statement in shell scripting is used to test
conditions and execute commands based on the result of the test. In
shell scripts, it is possible to nest multiple if
statements within each other to create complex conditions and execute
different sets of commands based on multiple conditions. These nested if
statements are referred to as nested if
statements.
Here's a simple example to demonstrate the use of a nested if
statement:
#!/bin/bash
# Check if a number is positive, negative, or zero
read -p "Enter a number: " number
if [ $number -gt 0 ]
then
echo "The number is positive"
if [ $((number % 2)) -eq 0 ]
then
echo "The number is also even"
else
echo "The number is odd"
fi
else
echo "The number is non-positive"
if [ $number -eq 0 ]
then
echo "The number is also zero"
else
echo "The number is negative"
fi
fi
Code Explanation:
In this example, the script first checks if the value of the number
variable is greater than 0. If the value is greater than 0, the script will execute the commands inside the first if
block and print "The number is positive". The script then checks if the value of number
is even or odd. If the value is even, the script will print "The number is also even". If the value is odd, the script will print "The number is odd".
If the value of number
is not greater than 0, the script will execute the commands inside the else
block and print "The number is non-positive". The script then checks if the value of number
is 0. If the value is 0, the script will print "The number is also zero". If the value is not 0, the script will print "The number is negative".
Nested if
statements are useful when you need to perform different actions based on multiple conditions. By nesting multiple if
statements within each other, you can create complex conditions and perform different actions based on the result of the tests.
In conclusion, nested if
statements are a powerful tool in shell scripting and can help you write scripts that are more flexible and adaptable to changing conditions. Whether you are working with user input, processing data, or managing system processes, nested if
statements can help you write efficient and effective shell scripts.
Options Used In Conditions And Control Structures
In shell scripting, conditions and control structures like if
, while
, and for
are used to control the flow of the script based on specific conditions. These structures allow you to test conditions, make decisions, and execute commands based on the results of the tests. There are several options available in shell scripting that can be used in conditions and control structures to enhance their functionality and make your scripts more flexible and efficient.
Here are some of the most commonly used options in shell scripting conditions and control structures:
-eq Option
The -eq
option is used to test if two values are equal. For example:
if [ "$a" -eq "$b" ]
then
echo "a and b are equal"
else
echo "a and b are not equal"
fi
Code Explanation:
In this example, the script tests if the values of a and b are equal. If the values are equal, the script will print "a and b are equal". If the values are not equal, the script will print "a and b are not equal".
-ne Option
The -ne
option is used to test if two values are not equal. For example:
if [ "$a" -ne "$b" ]
then
echo "a and b are not equal"
else
echo "a and b are equal"
fi
Code Explanation:
In this example, the script tests if the values of a and b are not equal. If the values are not equal, the script will print "a and b are not equal". If the values are equal, the script will print "a and b are equal".
-lt Option
The -lt
option is used to test if one value is less than another value. For example:
if [ "$a" -lt "$b" ]
then
echo "a is less than b"
else
echo "a is not less than b"
fi
Code Explanation:
In this example, the script tests if the value of a is less than the value of b. If the value of a is less than the value of b, the script will print "a is less than b". If the value of a is not less than the value of b, the script will print "a is not less than b".
-gt Option
The -gt
option is used to test if one value is greater than another value. For example:
if [ "$a" -gt "$b" ]
then
echo "a is greater than b"
else
echo "a is not greater than b"
fi
Code Explanation:
In this example, the script tests if the value of a is greater than the value of b. If the value of a is greater than the value of b, the script will print "a is greater than b". If the value of a is not greater than the value of b, the script will print "a is not greater than b".
-le Option
The -le
option is used to test if one value is less than or equal to another value. For example:
if [ "$a" -le "$b" ]
then
echo "a is less than or equal to b"
else
echo "a is greater than b"
fi
Code Explanation:
In this example, the script tests if the value of a is less than or equal to the value of b. If the value of a is less than or equal to the value of b, the script will print "a is less than or equal to b". If the value of a is greater than the value of b, the script will print "a is greater than b".
-ge Option
The -ge
option is used to test if one value is greater than or equal to another value. For example:
if [ "$a" -ge "$b" ]
then
echo "a is greater than or equal to b"
else
echo "a is less than b"
fi
Code Explanation:
In this example, the script tests if the value of a is greater than or equal to the value of b. If the value of a is greater than or equal to the value of b, the script will print "a is greater than or equal to b". If the value of a is less than the value of b, the script will print "a is less than b".
-z Option
The -z
option is used to test if a value is empty or zero. For example:
if [ -z "$a" ]
then
echo "a is empty or zero"
else
echo "a is not empty or zero"
fi
Code Explanation:
In this example, the script tests if the value of a is empty or zero. If the value of a is empty or zero, the script will print "a is empty or zero". If the value of a is not empty or zero, the script will print "a is not empty or zero".
-n Option
The -n
option is used to test if a value is not empty or not zero. For example:
if [ -n "$a" ]
then
echo "a is not empty or zero"
else
echo "a is empty or zero"
fi
Code Explanation:
In this example, the script tests if the value of a is not empty or zero. If the value of a is not empty or zero, the script will print "a is not empty or zero". If the value of a is empty or zero, the script will print "a is empty or zero".
-f Option
The -f
option is used to test if a file exists and is a regular file. For example:
if [ -f "$file" ]
then
echo "file exists and is a regular file"
else
echo "file does not exist or is not a regular file"
fi
Code Explanation:
In this example, the script tests if the file file exists and is a regular file. If the file file exists and is a regular file, the script will print "file exists and is a regular file". If the file file does not exist or is not a regular file, the script will print "file does not exist or is not a regular file".
-d Option
The -d
option is used to test if a file exists and is a directory. For example:
if [ -d "$dir" ]
then
echo "dir exists and is a directory"
else
echo "dir does not exist or is not a directory"
fi
Code Explanation:
In this example, the script tests if the directory dir exists and is a directory. If the directory dir exists and is a directory, the script will print "dir exists and is a directory". If the directory dir does not exist or is not a directory, the script will print "dir does not exist or is not a directory"
-x Option
The -x
option is used to test if a file exists and is executable. For example:
if [ -x "$file" ]
then
echo "file exists and is executable"
else
echo "file does not exist or is not executable"
fi
Code Explanation:
In this example, the script tests if the file file exists and is executable. If the file file exists and is executable, the script will print "file exists and is executable". If the file file does not exist or is not executable, the script will print "file does not exist or is not executable".
-s Option
The -s
option is used to test if a file exists and is not empty. For example:
if [ -s "$file" ]
then
echo "file exists and is not empty"
else
echo "file does not exist or is empty"
fi
Code Explanation:
In this example, the script tests if the file file exists and is not empty. If the file file exists and is not empty, the script will print "file exists and is not empty". If the file file does not exist or is empty, the script will print "file does not exist or is empty".
&& Option
The &&
operator is used to execute commands only if the previous command was successful. For example:
mkdir dir && echo "Directory created successfully"
Code Explanation:
In this example, the script will only print "Directory created successfully" if the mkdir command was successful in creating the directory dir.
|| Option
The ||
operator is used to execute commands only if the previous command was not successful. For example:
mkdir dir || echo "Directory creation failed"
Code Explanation:
n this example, the script will only print "Directory creation failed" if the mkdir command was not successful in creating the directory dir.
for loops
The for
loop is used to repeat a set of commands for a specified number of times or for each item in a list. For example:
for i in 1 2 3 4 5
do
echo "Iteration number $i"
done
Code Explanation:
In this example, the script will print "Iteration number 1", "Iteration number 2", and so on, up to "Iteration number 5".
while loops
The while
loop is used to repeat a set of commands while a specified condition is true. For example:
count=1
while [ $count -le 5 ]
do
echo "Iteration number $count"
count=$((count + 1))
done
Code Explanation:
In this example, the script will print "Iteration number 1", "Iteration number 2", and so on, up to "Iteration number 5". The condition for the while loop is specified as [ $count -le 5 ], which means that the loop will continue to run as long as the value of count is less than or equal to 5.
Reference Books
Here are the books I’ve used as references for writing this article,
please feel free to read them If you don’t want your knowledge to be
limited to this article alone.
Post a Comment