0% found this document useful (0 votes)
5 views11 pages

Shell Programming Examples and Scripts

This document contains various shell programming examples, including basic arithmetic operations, searching for words in files, checking even/odd numbers, calculating factorials, generating multiplication tables, and more. Each example includes code snippets and sample outputs demonstrating the functionality. The document serves as a practical guide for learning shell scripting techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views11 pages

Shell Programming Examples and Scripts

This document contains various shell programming examples, including basic arithmetic operations, searching for words in files, checking even/odd numbers, calculating factorials, generating multiplication tables, and more. Each example includes code snippets and sample outputs demonstrating the functionality. The document serves as a practical guide for learning shell scripting techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

UNIT-2

SHELL PROGRAMMING [Link] CHINTAIAH

1.#TO DISPLAY ADDITION OF TWO NUMBERS

echo"enter values of a,b:"


read a
read b
echo"Addition:`expr $a + $b`"

O/P:
enter values of a,b:
2
3
Addition:5

2.#TO DISPLAY THE SUM OF TWO NUMBERS

Description : In this program from the user input we are taking one input and the
second input is assigned to some value which read only type, means we can not change
that value except using it.

echo "enter value of a:"


read a
b=20
readonly b
echo "addition:`expr $a + $b`"
O/P:
enter value of a:
12
addition:32

3.#TO SEARCH FOR A WORD IN A FILE

echo "enter the word to be searched:"


read wordname
echo "enter the file name:"
read filename
grep "$wordname" $filename

O/P: enter the word to be searched:we


enter the file name: space
we are doing unix lab
4.#TO CHECK WEATHER THE GIVEN NUMBER IS EVEN OR ODD

echo "ENTER ANY NUMBER”


read a
if test `expr $a % 2` -eq 0
then
echo "$a is even number"
else
echo "$a is odd number"
fi

O/P: enter any number: 4


4 is even number

5.#TO FIND THE FACTORIAL OF A GIVEN NUMBER

echo "enter any number:"


read a
b=1
while test $a -gt 1
do
b=`expr $a \* $b`
a=`expr $a - 1`
done
echo "factorial of the number:" $b

O/P: enter any number:5


factorial of the number: 120

6.#MULTIPLICATION TABLE OF GIVEN NUMBER

clear
echo "enter a number:"
read a
echo "multiplication of the table for$a"
for((i=0;$i<=20;i++))
do
c=`expr $a \* $i`
echo "$a * $i = $c"
done
O/P:
enter a number: 3
multiplication of the table for 3
3*0=0
3*1=3
3*2=6
3*3=9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
3 * 8 = 24
3 * 9 = 27
3 * 10 = 30
3 * 11 = 33
3 * 12 = 36
3 * 13 = 39
3 * 14 = 42
3 * 15 = 45
3 * 16 = 48
3 * 17 = 51
3 * 18 = 54
3 * 19 = 57
3 * 20 = 60

7.# TO CHECH WEATHER THE GIVEN NUMBER IS PRIME OR NOT

echo "Enter any number:"


read n
i=1
c=0
while test $i -le $n
do
if test `expr $n % $i ` -eq 0
then
c=`expr $c + 1`
fi
i=`expr $i + 1 `
done
if test $c -eq 2
then
echo " $n is a prime number:"
else
echo " $n is not a prime number "
fi
O/P:
Enter any number:
7
7 is a prime number:

8.# MENU PROGRAMS


clear
echo "Enter two numbers:"
read a b
echo "MENU"
echo "[Link]"
echo "[Link]"
echo "[Link]"
echo "[Link]"
echo "Enter your choice"
read ch
case $ch in
1)c=`expr $a + $b`
echo "Addition of given numbers is "$c
;;
2)c=`expr $a - $b`
echo "subtraction of the numbers is "$c
;;
3)c=`expr $a \* $b`
echo "multiplication of the numbers is "$c
;;
4)c=`expr $a / $b`
echo "division of the numbers"$c
;;
*)
echo "your choice is wrong ,enter right choice"
esac

O/P:
Enter two numbers:
23
MENU
[Link]
[Link]
[Link]
[Link]
Enter your choice
1
Addition of given numbers is 5
9.#TO FIND THE REVERSE OF THE GIVEN NUMBER

clear
echo "enter any number:"
read n
sum=0
while test $n -ne 0
do
r=`expr $n % 10`
x=`expr $sum \* 10`
sum=`expr $x + $r`
n=`expr $n / 10`
done
echo "reverse of the given number:" $sum

O/P:
enter any number: 123
reverse of the given number: 321

10.# INSERTING ELEMENTS INTO THE LIST

clear
echo "Enter array size"
read n
echo "Enter $n elements"
for((i=0;$i<$n;i++))
do
read first[$i]
done
echo "enter the new element"
read element
echo "enter the position to insert"
read position
echo "elements after insertion"
for((i=0;$i<$n;i++))
do
if test $position -eq $i
then
k=$i
second[$k]=$element
k=`expr $k + 1`
second[$k]=${first[$i]}
k=`expr $k + 1`
i=`expr $i + 1`
fi
second[$k]=${first[$i]}
k=`expr $k + 1`
done
for((i=0;$i<$k;i++))
do
echo "${second[$i]}"
done

O/P: Enter array size : 5


Enter 5 elements
1
2
3
4
6
enter the new element : 5
enter the position to insert : 2
elements after insertion
1
2
5
3
4
6

11.#DELETING ELEMENTS FROM THE LIST

clear
echo "enter array size"
read n
echo "enter $n elements"
for((i=0;$i<$n;i++))
do
read first[$i]
done
echo "Enter position value:"
read position
echo "After deletion"
for((i=0;$i<$n;i++))
do
if test $position -eq $i
then
k=$i
i=`expr $i + 1`
second[$k]=${first[$i]}
k=`expr $k + 1`
i=`expr $i + 1`
fi
second[$k]=${first[$i]};
k=`expr $k + 1`;

done
for((i=0;$i<$k;i++))
do
echo "${second[$i]}"
done

O/P:
enter array size : 5
enter 5 elements :
2
3
4
1
6
Enter position value: 3
After deletion
2
3
4
6

12.# SORTING OF ELEMENTS

Clear
echo "enter array size"
read n
echo "enter $n elements"
for ((i=0;$i<$n;i++))
do
read first[$i]
done
echo "AFTER SORTING: "
for ((i=0;$i<$n;i++))
do
for ((j=$i+1;$j<$n;j++))
do
if test ${first[$i]} -le ${first[$j]}
then
temp=${first[$i]}
first[$i]=${first[$j]}
first[$j]=$temp
fi
done
echo "${first[$i]} "
done

O/P:
enter array size : 5
enter 5 elements
2
1
3
5
4
AFTER SORTING:
5
4
3
2
1

13.# GREATEST OF THREE NUMBERS

clear
echo "Enter three numbers:"
read a b c
if test $a -gt $b && test $a -gt $c
then
if test $b -gt $c
then
echo $a $b $c
else
echo $a $c $b
fi
else
if test $b -gt $a && test $b -gt $c
then
if test $c -gt $a
then
echo $b $c $a
else
echo $b $a $c
fi
else
if test $a -gt $b
then
echo $c $a $b
else
echo $c $b $a
fi
fi
fi

O/P:Enter three numbers:


342
432

14.# TO DISPLAY THE FILE PERMISSIONS


clear
echo "Enter file name"
read filename
ch=0
until [ $ch -eq 4 ]
do
echo "[Link]"
echo "[Link] special file"
echo "[Link] permissions"
echo "[Link] permissions"
echo "[Link]"
echo "Enter your choice:"
read ch
case $ch in
1)if [ -d $filename ]
then
echo "It is a directory"
else
echo "Not a directory"
fi
;;
2)if [ -c $filename ]
Then
echo "It is a special character file"
else
echo "not a special character file"
fi
;;
3)if [ -r $filename ]
then
echo "it has read permissions"
else
echo "no read permissions"
fi
;;
4)if [ -w $filename ]
then
echo "it has write permissions"
else
echo "no write operations"
fi
;;
5)exit
;;

*)
echo "Wrong choice"
;;
esac
done

O/P:
Enter file name : space
[Link]
[Link] special file
[Link] permissions
[Link] permissions
[Link]
Enter your choice:
1
Not a directory

[Link]
[Link] special file
[Link] permissions
[Link] permissions
[Link]
Enter your choice:
3
it has read permissions
[Link]
[Link] special file
[Link] permissions
[Link] permissions
[Link]
Enter your choice:
5

You might also like