In the last blog, we learned about the basic bash shell scripting, variable declaration, taking user input& arguments, and If-else. in this video, I’ll share some more topics like If-elif, functions, loops, and some day-to-day tasks of a DevOps engineer and how they get automated with the help of bash shell scripting.
One of the most fundamental ideas in computer programming is decision-making. Like in any other programming language, if, if..else, if..elif..else, and nested if statements in Bash are used to execute code based on a certain condition.
in today's blog, I'll share about the Else-If also known as the Elif statement.
ELIF statement
The if..elif..else statement in Bash looks like this:
if CASE1
then
STATEMENTS1
elif CASE2
then
STATEMENTS2
else
STATEMENTS3
fi
The STATEMENTS1 will be carried out if the CASE1 evaluates to True. The STATEMENTS2 will be executed if the CASE2 evaluates to True. The STATEMENTS2 are carried out if none of the test commands return True.
The statement may contain one or more elif clauses. Otherwise, it is not required.
eg: to check the greatest of three numbers.
#!/bin/bash
a=100
b=400
c=300
if [[ $a -gt $b && $b -gt $c ]]
then
echo "A is the biggest"
elif [[ $b -gt $a && $b -gt $c ]]
then
echo "B is the greatest"
else
echo "C is the greatest"
fi
-gt
is used for greater than condition and &&
is used for Logical AND
.
Output:
Loops
In BASH scripts, loops and conditional statements can be used to easily solve some challenging, repetitive problems.
Depending on the use case and the issue it is trying to solve, there are a few different ways to use for loops.
Simple For loop
Range-based for loop
Array iteration for loops
C-Styled for loops
Infinite for loop
Simple For loop
To execute a for loop, we can write the following syntax:
#!/bin/bash
for n in a b c;
do
echo $n
done
for eg: we need to print numbers from 1-10.
#!/bin/bash
for ((i=0; i<=10; i++));
do
echo $i
done
Now these are some basics. Assume we need to create files and directories in increments of one to twenty.
So, we will use touch
command to create new files and mkdir
to create new directories with some iterations.
touch file-{1..20}.txt
Output:
Now let's say we need to list all the text files only in a given directories.
for FILE in *.txt
do
echo $FILE
done
Functions
A bash function is a technique for grouping reusable bits of code under one name for later use. The bash function is like a script within a script.
syntax:
<function name> ()
{
<commands>
}
Let's take a good example of creating a user using a function in a script, and if the user is successfully created, then print a message that "user created successfully."
#!/bin/bash
add_user()
{
USER=$1
PASS=$2
useradd -m -p $PASS $USER && echo "User created Sucessfully"
}
#MAIN
add_user RitikGupta Password@123
to verify if the user gets created or not. In Linux we have one /etc folder and within that there is a file named passwd
which stored all the user info in it.
using the cat /etc/passwd
we can check if the user is created or not in the last the new user will get created.
Task 1:
You have to do the same using Shell Script i.e. using either Loops or command with start day and end day variables using arguments
So write a bash script, createDirectories.sh that, when executed with three given arguments (directory name, start number of directories, and end number of directories), creates the specified number of directories with a dynamic directory name.
Example 1: When the script is executed as
./
createDirectories.sh
day 1 90
then it creates 90 directories as day1 day2 day3 .... day90
Example 2: When the script is executed as
./
createDirectories.sh
Movie 20 50
then it creates 50 directories as Movie20 Movie21 Movie23 ...Movie50
#!/bin/bash
for (( i=$2; i<=$3; i++))
do
mkdir $1$i
done
Output:
Task 2:
Create a Script to backup all your work done till now.
so, lets create one folder which stores our backups.
and let's say we need to create a backup of our Scripts folder.
The script for this task will be.
Output:
our backups will get stored in /root/backups
Thanks for reading! I hope you understood these concepts and learned something.
If you have any queries, feel free to reach out to me on LinkedIn.