What are Shell Scripts

Posted By : Aditi Nahar | 21-Dec-2017

A shell is a software in a Linux or Unix operating system which provides a text-only user interface to enter commands (i.e. instructions). These commands are then executed and the output is displayed on a screen. In addition to this, a shell can also run commands stored in a file. And this mode of execution is known as shell scripting.


There are thousands of Linux commands and it is really difficult to remember them all. So here comes the need for automation. Shell scripts are short programs which are extremely useful for automating tasks on Linux or Unix operating systems. It is a simple text file containing a sequence of commands to be executed in one go by the operating system which otherwise has to be entered from the keyboard one at a time.


The shells used on Linux systems contains a built-in programming language i.e. shell programming language or shell scripting language and is used to create shell scripts. 


Out of many advantages of shell scripts we have already seen one i.e., it automates frequently executed tasks. Others are as follows:
1) They are very easy to develop.
2) Used to combine multiple sequences of commands into a single command.
3) As it is a text file, it can be easily viewed and thus diagnosing problems with it is easy and fast.
4) They can be transferred and executed on other Linux or Unix operating systems.


Let's see below some of the Use of shell scripts:
1) Application startup scripts.
2) Automating the setup process of application.
3) Gathering system/component information after a specified task.
4) Deleting desired files after a specified tasks eg server log files.


Now we'll understand this with the simple task of 'How to grep load and CPU utilization of a machine for half an hour with a regular interval of 5 minutes'.

Step 1: Create a file named load.sh

Step 2: Write the following commands into it:
#! /bin/bash
tdate=`date '+%D %H:%M:%S'`
load=`cat /proc/loadavg | awk '{print $1}' `
totalcpu=`top -b n2 | grep 'Cpu(s)' | cut -d',' -f1 | cut -d':' -f2 | tail -1`

echo -e "$tdate $load $totalcpu
sleep 300

Explanation of the above script:

date '+%D %H:%M:%S - this will give server date, hour, minute and second.


/proc/loadavg -
/proc is a virtual filesystem which is sometimes referred as process information pseudo filesystem. 
/proc/loadavg is the system's 'load average'; 3 indicators which depict the amount of work system did during last 1, 5 and 15 minutes.


awk '{print $1}' -
The basic command for awk is:
awk 'pattern {action}' input file > output file 
$1 refers to first column


top -b n2 command -
This will execute the top command twice (n2 = 2 iterations), 'b' is used for the bash script.


grep 'Cpu(s)'-
This will grep the line containing 'Cpu(s)'.


cut -d',' -f1 and cut -d':' -f2
This will print selected parts of the line. 'd' is for a delimiter. 'f' is the flag for selecting the field.


tail -1
This will display the last line of the file.

This will show the output.


sleep 300
This delays execution of the script for 5 minutes (i.e 300 sec).

Step 3: run the script with the following command and write the result into a CSV file named Result.csv
sh load.sh >> Result.csv

Step 4: Check the readings in Result.CSV file



Happy Scripting.

About Author

Author Image
Aditi Nahar

Aditi is a certified Software Tester and have experience in manual testing. She believes in continuous learning and doing the work with full dedication.

Request for Proposal

Name is required

Comment is required

Sending message..