Sitemap
Analytics Vidhya

Analytics Vidhya is a community of Generative AI and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Getting Started With Shell Scripting

5 min readSep 11, 2021

--

Hey, everyone! In this blog, we will learn about something that interests many of us: Shell Scripting.

Press enter or click to view image in full size
Photo by Sai Kiran Anagani on Unsplash

Remember when in movies we see a hacker crunching keys on the keyboard and typing something on a black screen, How cool that feels, doesn't it? So let's first understand what exactly is Shell Scripting.

There are two ways in which you can usually interact with any OS:-

  1. Graphical User Interface or GUI
  2. Command Line Interface or CLI

GUI is what most of us use in which there’s a graphical view and with the help of a mouse, you interact with the OS.

CLI is when there’s a black screen (instead of a GUI) called a terminal and you type your commands in the terminal to do whatever you want to do.

Now if anyone’s wondering which one is more powerful then undoubtedly the answer is CLI because you have more freedom and access in CLI and you can type your own customized commands to access anything and do anything.

Shell Scripting is made up of two words, Shell and Scripting. So what is their meaning?

What is Shell?

So when you type your commands in the terminal there must be some program that accepts those commands, runs them, and prints the output on a black screen, right? and that program is known as Shell.

There are various types of shell available in the market like Bourne shell (sh), C shell (csh), TC shell (tcsh), Korn shell (ksh), Bourne Again SHell (bash). But the ones which are widely used are Bourne shell (sh) and Bourne Again SHell (bash). Bash is like the upgraded version of sh. And in windows we have PowerShell. Although shell scripting is more popular in Linux.

What is Scripting?

Now there might be some activities which you need to perform repetitively and typing commands every time for them would be tiresome so you can bunch all those commands in one file and then execute that file to automate your tasks/activities and this is known as Scripting. The extension used for the file is ‘.sh’ eg. test.sh

So all shell scripting really is, is just bunching all the required commands and applying logic to them to form a nice workflow in order to automate your tasks. :)

So let's learn Shell Scripting. But first you need to know some basic Linux commands. So take a look at below commands which are frequently used in Linux terminal.

Pwd : Current Directorymkdir : Create new directorycd : Change DirectoryLs : Lists all files in a Directoryls -la : Lists all files with hidden filesLs -L : Lists files with size & other detailstouch : Create an empty Filemv : Move/rename a filecp : Copy a filecp -r : Copy a directoryrm : Delete a filerm -r : Delete a directorymore : Read the whole file contenttail : Read the last 10 lines, usually for log filesgrep : Find pattern or text inside a filehistory : History of recently typed commandstop : lists top 10 processes consuming memorycomm : Lists files with detailsdf & du : Check disk free space & disk useddate : Shows current date/timeuptime : Shows current uptimefinger user : Displays information about

Shell scripting is just like any other programming language and we use the same logic in it as any other programming language.

We can write the functions in it, if-elif-else conditions, case, and of course loops.

Now let's take two use cases for shell scripting so that we can apply some logics in it and learn while doing it.

A) Creating a function that takes the input from a user as a name and greets them.

#!/bin/bashecho "Hey, there! What is your name?"read nameecho "Nice to meet you, $name"

Here ‘#!’ is known as SheBang or HashBang. It is used to tell OS which interpreter to use while running this script. And here we are telling it to use bash. (/bin/bash is the path for bash)

Then we are using ‘echo’ to ask the name of a user and later using ‘read’ to capture the input from user.

And lastly we are printing a message “Nice to meet you, $name” where ‘$’ is used to tell our script that ‘name’ is a variable and not string.

B) Problem Statement:- Create an IP sweeper which will sweep the IPs of all active devices in a network

It's pretty simple to do. We just need to run a for loop and ping each IP in a given network and if that IP is alive then our output will be something like this -“ 64 bytes from 200-147-67-142.static.uol.com.br (200.147.67.142): icmp_seq=20 ttl=241 time=253 ms

So the alive IPs will have ’64 bytes’ in them so we will use it to grep the alive IPs output

#!/bin/bashif ["$1" == ""]
then
echo "Oops! You forgot an IP Address"
echo "Syntax: ./ipsweep.sh" 192.168.4"
else
for ip in `seq 1 254`; do
ping -c 1 $1.$ip | grep "64 bytes" | cut -d " " -f 4 | tr -d ":" &
done
fi

Then we used the ‘cut’ command to pick the fourth number of field from the above sentence which is (200.147.67.142):

After this, we used the ‘tr’ command to get rid of that colon (:) present at the end of IP

And finally, we used ‘&’ so that these all processes will run simultaneously in the background and it will execute fastly.

If you do not use the ‘&’ in last then the process will become very much slow. You can see the difference by yourself by first running the script without ‘&’ and then with ‘&’.

Now we run these commands inside of a for loop which is also running inside of an if-else condition to check if the user has provided a network range field or not. If it's not given then the script prints on terminal “Oops! You forgot an IP Address” with the correct syntax to run the script.

And thats it. You have successfully completed this class of shell scripting! I hope now you have at least a little bit of idea about what is shell scripting or at least curios about it to learn more.

So that's it for this one and I'll meet you in the next blog. (P.S. I know it's not a youtube video but I like to say that. :) )

Thanks for reading till here and if you have any queries feel free to comment here.

--

--

Analytics Vidhya
Analytics Vidhya

Published in Analytics Vidhya

Analytics Vidhya is a community of Generative AI and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Saurabh Rohankar
Saurabh Rohankar

Written by Saurabh Rohankar

Its never too late, my friend! 😉