<

Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x

Shell Scripting Basics

Published on
4,836 Points
1,836 Views
Last Modified:
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
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 18

Expert Comment

by:Simon
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.
0
 
LVL 10

Expert Comment

by:Carlos Ijalba
@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.
0

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Join & Write a Comment

Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Other articles by this author
Suggested Courses

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month