Shell Scripting Basics

Smita MelinmaniTechnology Lead
Published:
Updated:
Why Shell Scripting?
Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line interpreter and “Script” refers to combination of sequence of commands in single file to perform desired action just like batch files in DOS. You can initiate the shell script from command line by specifying the name of shell script. This saves lot of time as we can automate the tasks and can take inputs from user and send output on screen or email too.

Getting started with Shell Scripting
Before we move on to actual shell scripting here are things you should know
  1. Using text editors like vi or vim editors
  2. Basic commands
There are different types of shells available like bash, bourne, korn, C shell etc. I am making use of bash shell in my examples.

Permissions Required
Before you begin check that you have execute permissions on file. You can give permissions by using chmod command.

Writing a Shell script
Let us create a basic script to print todays date. Steps are:
  • Open the file to write a script using vi
    vi MyFirstScript.sh
  • Write your code in this file to accomplish some task
  • Save the file
You can execute now with below command
./MyFirstScript.sh

Example:
vi MyFirstScript.sh
#!/bin/bash
                      echo "This is my Shell Script"
                      echo "Todays Date is"
                      date "+%m/%d/%y"

Open in new window

The first line of the script is to use bash shell. The shell ignores anything after #, but #! as a prefix to the name
of the executable is interpreted as that will actually process the lines that follow.
#!/bin/bash
                       

Open in new window

The shell scripting can be used to work smarter. Shell scripting is useful can be used for administration. It makes life easy for them. Let us see an example which is common task for system administrators. 

Identifying top CPU utilizing processes. 
As a System Administrator you may have to find the processes or user programs which are consuming lot of CPU resources.
Monitoring Performance of System is one of day to day activity for sysadmin to diagnose the issues.

This script is helpful when you see system is slow at specific interval and you want to identify which user processes are consuming the resources and making the system to be slow at this point of time. 

You can schedule this in your cron a Job Scheduler at the time when your system is behaving differently.

I am making use of well known below two commands in the script.

1. Top Command to know the Top CPU Processes.
2. ps command to know which processes in detail.

So here is the script you can run and view the logfile for details.
Note: I have used FreeBSD machine to run this script and commands might slightly vary for different UNIX flavours. 
#!/bin/sh
                      echo "####################################################" >/home/user/performance.log
                      echo "Top CPU Processes at `date`"  >>/home/user/performance.log
                      echo "####################################################" >>/home/user/performance.log
                      echo  "" >>/home/user/performance.log
                      echo "####################################################" >>/home/user/performance.log
                      echo "Top Command Results at `date`"  >>/home/user/performance.log
                      echo "####################################################" >>/home/user/performance.log
                      
                      echo "" >>/home/user/performance.log
                      top >> /home/user/performance.log
                      
                      echo "####################################################" >>/home/user/performance.log
                      echo  "Processes Running at `date`" >>/home/user/performance.log
                      echo "####################################################" >>/home/user/performance.log
                      
                      ps -auxww >> /home/user/performance.log
                      
                      echo "" >>/home/user/performance.log
                      echo "####################################################" >>/home/user/performance.log

Open in new window


If you would like to see only top 5 processes then you can modify the top command like below:
top -n 5

Open in new window


-n represents to run in interactive mode.

If you also want to limit your output to print only few columns in top command like user,cpu%,command and time, then here you go:
top -n 5 | tail -7 | awk {'print $1,$9,$10,$11'}

Open in new window


Shell scripting is amazing way to make your OS work for you. Try some simple commands in a script and learn how to make use of best of it. 
 
0
2,838 Views
Smita MelinmaniTechnology Lead

Comments (2)

SimonPrincipal Analyst
CERTIFIED EXPERT

Commented:
Re "Using text editors like vi or vim editors"
- While these editors have advanced features, they also have a learning curve. For getting started I far prefer nano or pico, which are freely available if not already built-in.

To get to know the commands, refer to the manual pages for each command:
 man [command name]

..or Google them.
Carlos IjalbaSenior DevOps Engineer
CERTIFIED EXPERT
Top Expert 2014

Commented:
@SimonAdept, while using nano or pico maybe easier to learn, that will be all very nice in Linux, however try to use those editors in AIX, Solaris, HP-UX, OpenServer, UnixWare...

vi/vim are king for UNIX multiOS.

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.