Bash Scripting
Bash Scripting
It becomes very easy to control your whole OS using GUI interface. But at a lot of situation,
you will expected to control the whole OS using CLI interface. When can this happen?
Lets say we hosted our backend application on a AWS cloud server, now these server are present in a
very remote location, now if we have to interact with this server , then we need to control the OS of
this server from our machine and here CLI will only helps us as we won’t be having any GUI access to
the server.
In CLI interface, we use software like item , terminal , CMD , power shell etc. , which gives us an
interface to write some commands that will be running directly in our OS.
REPL Console
REPL stands for Read, Evaluate, Print, Loop. A REPL Console is a special type of console
which understand a particular programming language and every time we run it, it will expect us to
add one valid instruction to the console, it will then evaluate its output, print the output and then go
back to the same where it is expecting an input from us.
Languages like Python, Ruby, JS all have their respective REPL consoles.
Even the terminal which we see in our OS are REPL consoles, which understand bash scripting
language.
What is Shell?
A shell in the context of computing and operating system like Linux, is an interface that an
interface that allows you to access and interact services of your operating system. The primary
function of shell is to accept commands from user and then execute them.
What is Bash?
Bash is a type of scripting language that helps us to interface with a Linux shell. Bash stand for
Bourse Again Shell.
Using Bash, we can write Linux command in CLI software like item and terminal and also write end to
end programming script which can help us to automate a lot of things in our computer.
What is a bash command?
We can say that at some point of times , we might have to execute multiple bash command for
achieving a complex task. For example , We want to monitor that if the disk usage of our machine
goes beyond 90% , we should be mailed for this incident. If we are implementing this problem using
bash script then we might need a lot of lines to work together in a single logical piece , that’s where
bash script will help.
Lets say we save the above code in a new script.sh file. Now to run this file, we can use the following
command:
sh script.sh
Or
bash script.sh
With both of these commands we can actually run the bash script and see output on our screen.
Now in your machine there can be more than one shell scripting language available, like bash, zsh
etc. Sometime the default scripting language is not bash , so that’s why we have to separately
mention bash filename to enable it to run by bash script. Bash is an modification of the ‘sh’ shell
script.
If we don’t want to mention which scripting language to use in order to run the file and except it to
pick it from the code we can use shebang.
Shebang in script
Shebang looks like this : ’#!’ This shebang is added on the script and then we can mention
that path of the scripting language we want to get execute by
Here we have added some bash code and at the top of the file added ‘#!/bin/bash’ . So we passed
the path of the interpreter which will be used to run the code, instead of picking the default one
from system.
Because we have mentioned the interpreter, we can run the file independently.
./script.sh
But this command will give you error . Why? Because the current script file is not executable.When
we say bash script.sh then bash interpreter I directly executeable in the terminal. To make our file
directly executable we can change it’s permission.
chmod +x script.sh
./script.sh
This command will start running the file, at tp because of shebang pick the interpreter as bash and
then run the remaining code with bash.
To create a variable, we just give variables name put an equals and then give it a value
Threshold=80
This command creates a new variable threshold will value 80. Now if we want to use this variable at
any place, we have to prepend a $ before the variable name to access its value.
In this statement, $threshold helps us to access the value of the variable threshold.
Now to add a variable inside a bash we can use the same syntax.
The variables which we create in the terminal or in script will exist only in the same terminal session,
once we close our terminal or move to a new terminal window, those variables won’t be accessible.
Apart from variables created by us, there are some predefined variables as well like $USER, $PATH
that are already present in every terminal session we create. These are predefined in linux and serve
some specific purpose but if we want we can change their value.
If we want to see all the inbuild variables we can use the command :
env
This will list all the existing prebuild variables for us.
Subshells
If we want to store the output of a Linux command inside, we can do that using subshell.
Current_path=$(pwd)
--------------------------------------------------------------------------------------------------------------------------------------
Objective: Use variables in the script to automate the backup of a directory. The script should take
the directory name as a variable, compress the directory, and save the backup with a timestamp.
Here for multiplication we have use \ because * is consider for regex matching also in shell.
if [condition]; then
# Some algorithm
fi
So, any if block starts with the if keyword and end with fi keyword and in between we put the
condition and algorithm to execute when condition is true.
For example :
Here in the conditionals square bracket, we can use operator to prepare a condition which evaluates
to a Boolean true or false.
If we want to push multiple conditions in place then we can also use elif.
Note : eq =equals
ne=not equals
gt=greater than
lg=less then
Comand -v curl
pwd
echo $?
To get access of this code, there is a variable which stores after any command execute i.e. $?
while [ condition ]
do
#Some Logic
done
To write a while loop that prints counting of first 10 natural numbers we can do:
IMAGE
Problem Statement:
Create a Bash script that monitor a directory and continuously counts the number of files in it.
The script should print the file count every 10 second and if the directory becomes empty the script
should exit with a message indicate that the directory is empty.
Solution
for i in 1 2 3 4 5 6 7 8
do
sleep 1
done
Here we have a variable I, used inside the for loop which goes one by one to every single item of list
and take its value. Then just like while loops we use do and done to create a block of for which will
be executed again and again till the time I takes value of each and every single item.
Note:
In bash, if we want to represent a range of number we can use {x..y}. Here the range will start from x
and incrementally move up till y.
Problem:
Lets say we want to generate zip of every file inside a folder separately then we can use the below
script:
Problem:
Write a bash script that iterates through a directory containing text file. For each text file, then the
script should:
We can control how the stdout and stderr be handled in our Linux machine.
If we want to stream out correct output i.e. stdout in a file by dumping it we can use > or 1> .
With both of the above command codes, if our command runs properly then it will dump its log
inside output.txt but if the command fails, then the failure log or stderr will not be dumped in the
error.txt.
To dump the data of error, we can use 2> .
Here, if the command runs properly then logs will not be dumped in output.txt, but if the command
fails all the errors logs will be dumped.
If we don’t care and we want to dump everything i.e. stdout and stderr both then we can use &>
ls -l &> log.txt
Now, doesn’t matter stdout and stderr both will be dumped in log.txt .
If we want to separately stream both stdout and stderr then we can combine the usage of 1> and 2>.
Here all the stderr logs will be streamed to error.txt and stdout to success.txt.
Now to handle taking input from user in bash, we can use stdin. To trigger taking an input we have a
command called as read