Shell Scripting: Getting started with shell scripting
Hey, everyone! In this blog, we will learn about something that interests many of us: Shell Scripting.
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:-
- Graphical User Interface or GUI
- 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 the below commands which are frequently used in the Linux terminals.
Pwd : Current Directory
mkdir : Create new directory
cd : Change Directory
Ls : Lists all files in a Directory
ls -la : Lists all files with hidden files
Ls -L : Lists files with size & other details
touch : Create an empty File
mv : Move/rename a file
cp : Copy a file
cp -r : Copy a directory
rm : Delete a file
rm -r : Delete a directory
more : Read the whole file content
tail : Read the last 10 lines, usually for log files
grep : Find pattern or text inside a file
history : History of recently typed commands
top : lists top 10 processes consuming memory
comm : Lists files with details
df & du : Check disk free space & disk used
date : Shows current date/time
uptime : Shows current uptime
finger 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 logic 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/bash
echo "Hey, there! What is your name?"
read name
echo "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 that's 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 are at least are curious about it to learn more.