UNIT - 6 Shell Programming
UNIT - 6 Shell Programming
UNIT - 6 Shell Programming
BCA Semester - II
A shell is an environment in which we can run our commands, programs and shell scripts.
The shell provides an interface to the UNIX/Linux system. It gathers input from user and executes
programs based on that inputs. When program finishes its execution, it displays the program’s output.
Shells are interactive. Means they can accept commands from user (via keyboard) and execute them. But
if you use command one by one (sequence of ‘n’ number of commands), then you can store these
sequence of command to text file and tell the shell to execute this text file instead of entering
commands. This is known as shell script.
Ex. vi first.sh
# My first shell script
clear
echo “Hello World”
After saving the above script press esc and ( :wq ) you can run the script
$ sh first.sh
1
Shell Keywords:
Keywords are the words whose meaning has already been explained to the shell.
The keywords cannot be used as variable names because they are reserved words with reserved
meaning.
Shell Comments:
Example :
Shell Variables:
A variable is a simple way to refer to a block of data in memory that can be modified.
A variable in a unix script can be assigned any type of value, such as a text string or a number.
These types of variables are used in the shell script. It is created and maintained by user so it is a user
defined variable. These types of variables are also defined in lower case letters.
Syntax: variable_name=value
Value is assigned to the variable_name and value must be on the right side of = sign.
Example:
no=10 # it is right
[1] Rules for naming Variable Name (for User and System Variables) :
1. Variable name must begin with Alphanumeric character or underscore character “_”, followed
by one or more Alphanumeric characters.
2. Don’t put spaces on either side of equal sign when assigning values to the variables.
no=10 # its OK
no = 10 # Error
no= 10 # Error
no =10 # Error
no=10
No=11
NO=20
nO=30
4. You can define NULL variable as follows (NULL variable is a variable which has no value at
the time of definition)
stdnm=
3
stdnm=“ “
Syntax: $variable_name
rollno=7
echo $rollno
Unsetting or deleting a variable tells the shell to remove the variables from the list of variables that it
tracks. Once you unset the variable, you will not be able to access the stored value in the variable.
Ex. nm=hp
unset nm
echo $nm
echo Command:
Options:
read Command:
Above example would create stdno string type variable and input value of stdno from keyboard.
5
EX:
num1=10 num1=10
num2=0 num2=0
echo "enter value for num2" echo "enter value for num2"
read num2 read num2
echo "num1=$num1" echo "num1=$num1"
echo "num2=$num2" echo "num2=$num2"
n1=0 n1=0
n1=`expr $num1 + $num2` n1=$(($num1+$num2))
echo "sum=$n1" echo "sum=$n1"
6
Example
7
Example
name="raj"
class="bca"
per=65
if [ $per -ge 60 -a $per -lt 70 ]
then
echo "$name secure first class"
else
echo "not secure first class"
fi
8
If else if Case
n1=10
n2=50 echo "enter n1"
read a
n3=200
case $a in
if [ $n1 -gt $n2 -a $n1 -gt $n3 ] 1 ) echo "one" ;;
then 2 ) echo "two" ;;
echo "n1 is greater" * ) echo "invalid"
elif [ $n2 -gt $n1 -a $n2 -gt $n3 ] esac
then
echo " n2 is greater "
else
echo "n3 is greater "
fi
9
Looping Statements:
Example
10
Introduction of vi Editor:
The vi editor is the most popular editor in linux which is used to create and modify text files.
vi (pronounced vee-eye) is an editor that is fully in text mode, which means that all actions are carried
out with the help of text commands. The current version is really "vim", but to invoke it simply type
"vi". The text format that vi uses is the plainest elementary text format that exists, the ascii format.
$ vi filename
Modes in vi:
1. Command Mode:
When you first load the vi editor, you will be placed into command mode.
This mode allows typing commands. In this mode, all keys pressed by the user is interpreted to be
editor commands. In this mode, the keys pressed by the user is not displayed on the screen.
2. Insert Mode:
This mode permits insertion of new text or editing of existing text. All these operations can only
be performed by changing from command mode to insertion mode.
Everything that's typed in this mode is interpreted as input and finally it is put in the file.
3. The ex Mode:
vi uses the command line to display the messages and the commands.
All commands entered in the ex command mode are displayed in the command line.
11