0% found this document useful (0 votes)
8 views

Bash Scripting

The document provides an overview of Bash scripting and the Command Line Interface (CLI), emphasizing the importance of CLI for remote server management. It explains key concepts such as REPL consoles, shell, Bash commands, script creation, variables, conditionals, loops, and data streams. Additionally, it includes practical examples and problems to automate tasks using Bash scripts.

Uploaded by

Shekhar Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Bash Scripting

The document provides an overview of Bash scripting and the Command Line Interface (CLI), emphasizing the importance of CLI for remote server management. It explains key concepts such as REPL consoles, shell, Bash commands, script creation, variables, conditionals, loops, and data streams. Additionally, it includes practical examples and problems to automate tasks using Bash scripts.

Uploaded by

Shekhar Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Bash Scripting

Command Line Interface


To control the OS of a machine we have two main mechanisms:

1. GUI – Graphical User Interface


2. CLI - Command Line Interface

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.

Any bash script that we prepare will be having an extension of .sh .

How to write a bash script?


We can make a new file with an extension of .sh to make a bash script file. Any command that
we were able to directly execute in the terminal can be added as a code in this file to eventually run
it.

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

And now we can run the script independently

./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.

Variables in bash script


Sometimes we want to reuse a value at multiple places in our bash script , to do that we can create
variables. Variables in the bash script serve the same purpose as variables in any other language.

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.

echo “Value of threshold is $threshold”

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)

This will store output of pwd inside current_path.

--------------------------------------------------------------------------------------------------------------------------------------

Problem: Automate a Directory Backup Script

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.

Date and time:


Evaluation math expressions
If we want to evaluate math expression in shell, we can use the expr command and then any
arithmetic operation we can perform.

Here for multiplication we have use \ because * is consider for regex matching also in shell.

Conditionals and Decision in Bash Script


Being a programming language, bash script is capable of using conditional statements for decision
making process.

In bash, we have the if keyword that can help us to a conditional in place.

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

ge=greater than equals to

le=less than equals to

Comand -v curl

Exit codes in bash


Every command that we execute in linux shell has a dedicated exit code associated to it, that tells
whether the command executed successfully or not. This can helps us to put a programmatic way to
determine whether the command worked well or not otherwise most of the time we just look at the
logs and then determine this.

 Exit code 0 means the command was successful


 Non zero code means there was some issue

pwd

echo $?

To get access of this code, there is a variable which stores after any command execute i.e. $?

Loop in bash script


In bash we have support of loops using which we can do repeated task.
While loop
While loop takes condition and keeps on running it’s block of code till the time the condition
doesn’t become false.

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

1. We first of all take the directory as input.


2. In the while loop we are using ls -A that will also list hidden files from the input directory.
3. To do the file count we use ls -1 which print the output of ls command in multiple lines and
then we pipe this multiple output to the wc command which counts the words, character
and lines from the text. And because we are using wc with a-l flag it will only count the
number of line.

For loop in bash


We have while loop that keep on executing the task till the time condition is actually true. On the
other hand, we have for loop that can run again and again for each item in list or set of items.

for i in 1 2 3 4 5 6 7 8

do

echo "Value of i is ${i}"

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.

So we can modify our for loop using this range syantax.

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:

 Count the number of lines in the file.


 Append the line count to the end of the file.
Data Streams
Any text that receives as an output or pass as an input is classified into different stream of data.

 Standard Input (stdin) – Data stream where the input is received.


 Standard output(stdout) – Data stream where command output is sent.
 Standard error (stderr) – Data stream where any error is actually sent.

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>.

test.sh 1> success.txt 2> error.txt

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

You might also like