Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

UNIT - 6 Shell Programming

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Smt.

Chandaben Mohanbhai Patel Institute of Computer Applications, CHARUSAT

BCA Semester - II

CA117 Operating System Concepts

Unit: 6 Shell Programming

What is Shell Script?

A shell is an environment in which we can run our commands, programs and shell scripts.

“Shell script is a series of commands written in plain text file.”

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.

How to write a shell script?

Use any editor like vi to write shell script as:


vi script-name.sh

Execute a script as:


sh script-name.sh

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

Script Commands Meaning


vi first Start vi editor
# My first shell script # followed by any text is considered as comment.
clear Clear the screen
echo “Hello World” To print message or value
Run the script [first]
sh first <enter> It is the command to execute the file first and
gives the output:
Hello World

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.

Keywords are also called as “Reserved Words”.

echo export done continue ulimit

read If for return umask

set else until trap exit

unset Fi case wait

readonly while esac eval

shift Do break exec

Shell Comments:

You can put your comments in your script by # symbol.

Syntax : # Comment Statement

Example :

# Program developed by : Hardik Patel

# Program to display largest of two numbers

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.

There are two types of variables:

[1] User Defined Variables:

They are of two types:


2
(A) Local Variables: A local variable is a variable that is represent within the current instance of the
shell. It is not available to the programs that are started by the shell. They are set at command prompt.
They are also defined in lower case.

(B) Shell Variables:

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.

Defining User Defined Variables:

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

10=no # Error, value must be on right side of = sign

[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.

Ex. HOME , nam , std , SYSTEM_VERSION

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

3. Variables are case sensitive.

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=“ “

5. Do not use ? , * etc for variable names.

[2] Accessing User Defined Variables:

Syntax: $variable_name

Ex.Define variable rollno as follows

rollno=7

To print the content of that variable rollno type

echo $rollno

It will print 7 as output.

echo “ Value of rollno=$rollno”

It will print Value of rollno= 7 as output.

[3] Destroying / Unsetting User Defined Variables:

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.

Syntax: unset variable_name

Ex. nm=hp

unset nm

echo $nm

The above example would not print anything.

echo Command:

Use echo command to display text or value of variable.

Syntax: echo [option] [String , Variable]

Options:

-n : Do not output the trailing new line.

-e : Enable interpretation of the following backslash escaped characters in the string :

\a : alert (bell) \b : backspace

\c : suppress trailing new line

\n : new line \r : carriage return


4
\t : horizontal tab \\ : backslash

Ex: echo “Hello world”

echo “Hello world \n this is os”

echo -e “Hello world \n this is os”

read Command:

Input the variable value from the keyboard.

Syntax: read variable_name

Example: read stdno

Above example would create stdno string type variable and input value of stdno from keyboard.

Operators in Shell Script:

Arithmetic Operators: Suppose a=10, b=20

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"

Relational Operators: Suppose a=10, b=20

6
Example

-eq -ne -gt


a=10 a=10 a=100
b=20 b=20 b=20
if [ $a -eq $b ] if [ $a -ne $b ] if [ $a -gt $b ]
#if [ $a==$b ] then then
then echo " a is not equal to b" echo " a is greater "
echo " a is equal to b" else else
else echo " a is equal to b" echo " a is not greater"
echo " a is not equal to b" fi fi
fi

-lt -ge -le


a=10 a=20 a=10
b=20 b=10 b=10
if [ $a -lt $b ] if [ $a -ge $b ] if [ $a -le $b ]
then then then
echo " a is less than b" echo " a is greater or equal to b" echo " a is less or equal to
else else b"
echo " a is not less than b" echo " a is not greater or equal to b" else
fi fi echo " a is not less or equal
to b"
fi

Logical Operators: (Boolean operators) suppose a=10, b=20

7
Example

-o (OR operator) -a (AND operator)


name="raj" name="raj"
class="bca" class="bca"
mark=20 mark=20
if [ $class == "bca" -o $mark -gt 50 ] if [ $class == "bca" -a $mark -gt 50 ]
then then
echo $name echo $name
else else
echo "False" echo "False"
fi fi

-a (AND operator for range)

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

Decision Making Statements:

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

FOR While Until


n=1 n=1
for I in 1 2 3 4 5 while [ $n -le 10 ] until [ $n -gt 10 ]
do do do
echo “I=$i” echo "n=$n" echo "n=$n"
done n=$(($n+1)) n=$(($n+1))
done done

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.

The syntax to launch vi editor is as follows:

$ vi filename

Modes in vi:

There are three 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.

It is the default mode when user starts vi editor.

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.

To switch to the insertion mode, press the ‘i’ key.

Everything that's typed in this mode is interpreted as input and finally it is put in the file.

The insertion mode is also known as input-text mode.

3. The ex Mode:

This mode permits us to give commands to the command line.

The bottom line of the vi screen is called the command line.

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

You might also like