Shell Scripting With Examples

Posted byShailesh Posted onJuly 25, 2011 Comments0

No programming language is perfect. There is not even a single best language, there are only languages well suited or perhaps poorly suitedfor particular purposes………..Genowin Technologies

What Is a Shell?
Ans-The shell is a user program or it is an environment provided for user interaction.
It is a command language interpreter that executes commands read from the standard input device such as keyboard or from a file.
The shell gets started when you log in or open a console (terminal).

Several shells are available for Linux including
BASH ( Bourne-Again SHell ) – Most common shell in Linux. It’s Open Source.Default Installed.
CSH (C SHell) – The C shell’s syntax and usage are very similar to the C programming language.
KSH (Korn SHell) – Created by David Korn at AT & T Bell Labs. The Korn Shell also was the base for the POSIX Shell standard specifications.
TCSH – It is an enhanced but completely compatible version of the Berkeley UNIX C shell (CSH).

Shell Prompt
There are various ways to get shell access:
Terminal – Linux desktop provide a GUI based login system. Once logged in you can gain access to a shell by running X Terminal (XTerm), Gnome Terminal (GTerm), or KDE Terminal (KTerm) application.
Connect via secure shell (SSH) – You will get a shell prompt as soon as you log in into remote server or workstation.
Use the console – A few Linux system also provides a text-based login system. Generally you get a shell prompt as soon as you log in to the system.

How do I find Out My Current Shell Name?
To find all of the available shells in your system, type the following command:
cat /etc/shells

Basic Command Line Editing
You can use the following key combinations to edit and recall commands:
CTRL + L : Clear the screen.
CTRL + W : Delete the word starting at cursor.
CTRL + U : Clear the line i.e. Delete all words from command line.
Up and Down arrow keys : Recall commands (see command history).
Tab : Auto-complete files, directory, command names and much more.
CTRL + R : Search through previously used commands (see command history)
CTRL + C : Cancel currently running commands.
CTRL + T : Swap the last two characters before the cursor.
ESC + T : Swap the last two words before the cursor.
CTRL + H : Delete the letter starting at cursor.
……………………………………………………………………………………………………………………………..
What is Shell Scripting?
Shell Scripting- A script is nothing more than a list of system commands stored in a file. At the very least, this saves the effort of retyping that particular sequence of commands each time it is invoked.

How to write shell script?

  • First we need to use any command editor like vi, vim.
  • Than write some command in that editor, we can add ‘#’ for any comment (to make program more user friendly).
  • Save and exit.
  • Change mode (to make executable) with command “chmod +x filename”.
  • Run with command “./filename”.

Example
$ vi first
# My first shell script
clear
echo ” Hello Linux world…its my first script”
………………………………………………………………………………………………………………………………
Why shell scripting?
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.

Examples
1)Write following shell script, save it, execute it and note down the it’s output.

$ vi ginfo
#
#
# Script to print user information who currently login , current date & time
#
clear
echo “Hello $USER”
echo “Today is \c “;date
echo “Number of user login : \c” ; who | wc -l
echo “Calendar”
cal
exit 0

Q.2. How to write shell script that will add two nos, which are supplied as command line argument, and if this two nos are not given show error and its usage

Answer:

#!/bin/bash
# Q1.Script to sum to nos#
if [ $# -ne 2 ]
then
echo “Usage – $0   x    y”
echo ”        Where x and y are two nos for which I will print sum”
exit 1
fi
echo “Sum of $1 and $2 is `expr $1 + $2`”

Q.3. Script to  read name from key-board.

Answer:

#
echo “Your first name please:”
read fnameecho “Hello $fname, Lets be friend!”
Run it as follows
$ chmod +x sayH
$ ./sayH

Q.4. Travel expenses Example

# if no vehicle name is given
# i.e. -z $1 is defined and it is NULL
# if no command line arg
if [ -z $1 ]
then
rental=”*** Unknown vehicle ***”
elif [ -n $1 ]
then
# otherwise make first arg as rental
rental=$1
fi

case $rental in
“car”) echo “For $rental Rs.20 per k/m”;;

“van”) echo “For $rental Rs.10 per k/m”;;
“jeep”) echo “For $rental Rs.5 per k/m”;;
“bicycle”) echo “For $rental 20 paisa per k/m”;;
*) echo “Sorry, I can not gat a $rental for you”;;
esac
Save it by pressing CTRL+D
$ chmod +x car
$ car van
$ car car

 

Thanks for visiting this site…………………..Welcome Genowin Technologies.
Happy To Help You ….
All Linux Solution & Training.
Genowin Technologies(www.genowin.com)

Category

Leave a Comment