You are on page 1of 5

Ex.No.2.

Shell Programming Date:10-01-22

Shell programming is a group of commands grouped together under single filename. The shell interprets
the input, takes appropriate action, and finally displays the output. Shell scripts are dynamically
interpreted, not compiled.

Types of shell

 Bourne shell sh
 C shell csh
 Korne Shell ksh

Creation and execution of shell scripts using command line editor:

1. creation
echo “please enter your name:”
read name
echo “hi! Welcome to this session $name”
2. Execution
please enter your name: os
hi! Welcome to this session os”

Assigning values to variable:


Variable=value

Displaying values of variables:


$ echo value of n is $n

Operators:

Arithmetic Operators provided by the shell are +,- * and /

Logical operators
-a and
-o or
! not

Relational operators
-eq : check fro equality of integers
-ne : check for inequality
-gt : check if one integer is greater than the other
-lt : check if one integer is lesser than the other
-ge : check if one integer is greater than or equal to the other
-le : check if one integer is lesser than or equal to the other.
-f : check if a file is an ordinary file
-d : check if a file is a directory
-r : check if a file is readable
-w : check if a file is write able
-x : check if a file is executable

String comparison operators


= equal to
!= not equal to

Logical operators
-a and
-o or
-! Not

Conditional execution operations


& used to execute a command on successful execution of another
command. || used to execute another command on failure of another
command.

Read command
Used to read the value of the shell variable from a user.
Comment statement
# this is a text program.
Programming language construct
1.a)if..then…else…fi b) if..then..elif..else ..fi
2.for…do…done
3.while..do..done
4.until…do..done
5.case …esac
1) if construct
useful for executing a set of commands based on the condition being true and alternate
set of commands to be executed if the condition is false. Ex. if (grep India countri.dat)

then
echo “pattern found”
else
echo “pattern not found”
fi

2) for construct
used to perform same set of operations on a list of values.
for variable in value1 value2 value3 …
do
Commands
done

Ex. for k in 1 2 3 4 5
do
echo “the number is $k”
echo “the square of the number is
`expr $k \* $k` “ done

3) while construct
Repeatedly executing group of commands as long as the condition is true.
while condition
do
Commandlist
done
Ex.to print 3 numbers
a=1
while [$a -le 3]
do
echo $a
$a=`expr $a+1`
done
o/p. 1 2 3
4) until construct
Repeatedly executing group of commands until a condition is true.
until condition
do
Commandlist
done
Ex.to print 3 numbers
a=1
until [$a -le 3]
do
echo $a
$a=`expr $a+1`
done
o/p. 1 2 3
5) case construct:
case value in
choice1) commands;;
choice2)commands;;
….
esac

Ex. $echo “enter a value”


read myval
case “$myval” in
0) echo zero;;
1) echo one;;
2) echo two;;
3) echo three;;
*) echo “invalid argument”;;
esac
Exercise:
1. Greatest among three
numbers Algorithm:
Step 1. Read 3 numbers n1,n2 and n3
Step 2. Check if n1 is greater than n2
check n1 is greater than n3 also
announce n1 as greatest
announce n3 as greatest
else, check whether n2 is greater than
n3 if yes, announce n2 as greatest
else announce n3 as greatest

2. Factorial of a given number


Algorithm:
Step 1. Read n
Step 2. Initialize fact to 1 and i to n
Step 3. Repeat the following until i>0
Assign fact * i to fact
Decrement i by 1

3. Sum of Odd numbers upton


Algorithm:
Step 1. Read n
Step 2. Initialize x=1 and sum=0
Step 3. Repeat the following until x < n
Assign sum + x to
sum Increment x by 2

4. Generation of Fibonacci numbers


Algorithm:
Step 1. Read n
Step 2. Initialize p=-1, q=1 and I=1
Step 3. Repeat the following until I < n
Assign p + q to r
Assign q to p
Assign r to q
Increment I by 1

5. Implement the Arithmetic calculator


Algorithm:
Step 1. Read a, b and option
Step 2. According to the option perform the operation
6. Write a Shell program to find the largest digit of a number
Algorithm:
Step 1: Get a number from the user
Step 2: Obtain individual digit for the above number using modulo operator
Step 3: Initialize variable max with first digit
Step 4; Compare the value of max with the other
digits, if the value of max is lesser update the value of
max
Step 5: Display the value of max

7. Check whether given string is a palindrome or not.


Algorithm:
Step 1. Read a String
Step 2. Find the length of the string
Step 3. Start reading from the last character to the first character and store it as a new
string in temp
Step 4. Compare both the strings, if it is same the given string is palindrome

8. Write a Shell program to find out the reverse of a given number.


Algorithm:
Step 1: Get a number from the user
Step 2: Set a loop upto the number is not equal to zero
Step 3: reminder=number%10
Step 4:rnum=rnum*10+reminder
Step 5: number=number/10
Step 6: if number==rnum print both are same
9. Write a Shell program for quadratic equation.
10. Write a Shell program to check whether the given number is ODD or EVEN.

You might also like