Wednesday, January 7, 2015

What is a shell script or shell scripting

A Shell script can be defined as - "a series of command(s) stored in a plain text file". A shell script is similar to a batch file in MS-DOS, but it is much more powerful compared to a batch file. Shell scripts are a fundamental part of the UNIX and Linux programming environment.  Normally shells are interactive. It means the shell will accept command from you (via keyboard) and execute them. However, if you store a sequence of commands to a text file and tell the shell to execute the text file instead of entering the commands, that is known as a shell program or shell script.

Each shell script consists of - Shell keywords such as if-else, do-while. Shell commands such as pwd, test, echo, continue, type. Linux binary commands such as w, who, free etc. Text processing utilities such as grep, awk, cut. Functions - add frequent actions together via functions. For example, /etc/init.d/functions file contains functions to be used by most or all system shell scripts in the /etc/init.d directory. Control flow statements such as if-then-else or shell loops to perform repeated actions.

Why we use Shell Scripting?
§  Shell scripts are useful for automating processes that you repeat at the prompt.
§  Shell scripts can take input from a user or file and output them to the screen.
§  Whenever you find yourself doing the same task over and over again you should use shell scripting, i.e., repetitive task automation.
§  Creating your own power tools/utilities.
§  Automating command input or entry.
§  Customizing administrative tasks.
§  Creating simple applications.
§  Since scripts are well tested, the chances of errors are reduced while configuring services or system administration tasks such as adding new users.

Shell Scripts used by me mostly:-
Shell scripting makes your Linux easy and here some examples of scripts that I use mostly while working on my Linux Box:
§  Find out what that site is running (just like or
§  Download RSS feeds and display them as you login or in your email (Updates from my favourite websites).
§  Find out the name of the MP3 file you are listening to (My own playlist).
§  Monitor your domain expiry date every day (100’s of Website I manage, for me it’s impossible to remember that which of the domain going to expire and when :P ).
§  Collecting the Web stats of my all websites and their comparison with previous day stats. This way I concentrate on Seo of my Projects.
§  A Notice Board, where I note down all task that have to do on any particular day or time, The script automatically popup a message on screen to remember me tasks.

Practical examples of shell scripting:-
§  Monitoring your Linux system.
§  Data backup and creating snapshots.
§  Dumping Oracle or MySQL database for backup.
§  Creating email based alert system.
§  Find out what processes are eating up your system resources.
§  Find out available and free memory.
§  Find out all logged in users and what they are doing.
§  Find out if all necessary network services are running or not. For example if web server failed then send an alert to system administrator via a pager or an email.
§  Find out all failed login attempt, if login attempt are continue repeatedly from same network IP automatically block all those IPs accessing your network/service via firewall.
§  User administration as per your own security policies.
§  Find out information about local or remote servers.
§  Configure server such as BIND (DNS server) to add zone entries.

How to use Bash scripts?
Open the terminal on your Linux machine and enter the command as follows:
./ [Arguments]
Here the script name is “cyberarmy”, format of script is “.sh” and [arguments] – If any input required (Depends upon script).

§  Easy to use.
§  Quick start and interactive debugging.
§  Time Saving.
§  Sys Admin task automation.
§  Shell scripts can execute without any additional effort on nearly any modern UNIX / Linux / BSD / Mac OS X operating system as they are written an interpreted language.
§  Compatibility problems between different platforms.
§  Slow execution speed.
§  A new process launched for almost every shell command executed.

No comments:

Post a Comment