Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 33

INDEX

S.No. TITLE
01 Program for adding two numbers
02 To check the number is even or not.

03 Program to check the largest of three numbers.


04 To check all even number in between one to fifty (1 - 50).

05 Program to display table.


Enter the marks of three subject using keyboard, find the percentage & then declare the
06
division.
07 Display a number in reverse order
08 Display the list.
09 Display the content of the file.
10 Display the sequence one to fifty.(1 – 50)
11 Display fibonacci series.

12 Write a program in UNIX Shell Programming to display number 1 to 10 using Until Loop.

13 By using Nesting Loop, display the pattern ;

14 Write a program to check a number whether it is an Armstrong number or not.

15 To check whether the number is Palindrome or not.

16 Write a Shell program to find the sum of square of individual digits of a number.

17 Write a Shell program to find the smallest number from a set of numbers.

18 Write a Shell program to find the smallest digit from a number.


19 Write a Shell program to show the get user input.
20 Write a Shell program to show the use of AND logic.
21 Write a Shell program to show the use of OR logic.
22 Write a Shell program to show the use of case statement.
23 Write a Shell program to combine string variables.
24 Write a Shell program to pass return value from function.
25 Write a Shell program to create a directory.
26 Write a Shell program to delete a file.
27 Write a Shell program to append a file.
28 Write a Shell program to find the factorial of a number.
29 Write a Shell program to find the swap two numbers.
30 Writ a Shell program to count the no. of characters in a given string

PROGRAM - 1
Objective: Program for adding two numbers.

Code:

echo “ Please Enter first number ”

read x

echo “ Please Enter second number ”

read y

Z=`expr $x + $y`

echo “ result =” $z

OUTPUT :-

PROGRAM – 2
Objective: To cheek the number is even or not.

CODE:

Echo “Enter a number”

read n

r=`expr $n % 2`

if test $r -eq 0

then

echo “Number is Even”

else

echo “Number is Odd”

fi

OUTPUT :-

PROGRAM – 3
Objective: Program to cheek the largest of three numbers.

CODE:

echo “ Please Enter three number ”

read x

read y

read Z

if [ $x -gt $y -a $x - gt $z ]

then

echo “$x is greater than $y and $Z”

elif [ $y -gt $x -a $y gt $z ]

then

echo “$y is greater than $x and $Z”

else

echo “$z is greatest”

fi

OUTPUT :-

PROGRAM – 4
Objective: To check all even number in between one to fifty (1 - 50)

CODE:

I=1

rem=0

While test $i - le 50

Do

rem=`expr $i % 2`

if test $rem -eq 0

then

echo $i

fi

i= `expr $i + 1`

done

OUTPUT :-

PROGRAM-5
Objective: Program to display table.

CODE:

echo “ Please Enter a number ”

read n

clear

i=1

while test $i le 10

do echo “$n * $i=`expr $n \* $i`”

i=`expr $i + 1`

done

OUTPUT :-

PROGRAM-6
Objective: Enter the marks of three subject using keyboard, find the percentage & then declare the division.

CODE:

echo “ Enter marks of Physics, Chemistry, Maths”

read Phy

read Chem

read Math

per=`expr $Phy + $Chem + $Math / 3`

if test $per -ge 75

then

echo “ Distinction”

elif test $per -lt 75 -a $per -ge 60

then

echo “ First Division”

elif test $per -lt 60 -a $per -ge 45

then

echo “ Second Division”

elif test $per -lt 45 -a $per -ge 33

then

echo “ Third Division”

else

echo “ Better Luck next time”

fi
OUTPUT :-

PROGRAM – 7
Objective : Display a number in reverse order.

CODE :

echo “Please enter a number”

read n

while [ $n -ne 0 ]

do

r=`expr $n % 10`

echo $r

n=`expr $n / 10`

done

OUTPUT :-

PROGRAM – 8
Objective : Display the list.

CODE :

for i in 11 22 33 44 55

do

echo “$i”

done

OUTPUT :-

PROGRAM – 9
Objective : Display the content of the file.

CODE :

for i in `cat sgrru`

do

echo “ $i ”

done

OUTPUT :-

PROGRAM – 10
Objective : Display the sequence one to fifty.(1 – 50)

CODE :

for i in { 1….50 }

do

echo “ $i ”

done

PROGRAM – 11
Objective : Display fibonacci series.

CODE :

echo “Enter number upto which you want to print Fibonacci series : ”

read n

x=0

y=1

c=1

echo $x

echo $y

while test $c -le $n

do

z=`expr $x + $y`

echo $z

x=$y

y=$z

c=`expr $c + 1`

done

OUTPUT :-

PROGRAM – 12
Objective : Write a program in UNIX Shell Programming to display number 1 to 10 using Until Loop.

CODE :

x=1

until test $x -gt 10

do

echo $x

x=`expr $x +1`

done

OUTPUT :-

PROGRAM – 13
Objective : By using Nesting Loop, display the pattern ;

*
* *
* * *
* * * *
* * * * *

CODE :

echo “ Enter character to display”


read ch
echo “ Enter number of rows”
read r
i=1
j=1
while test $i -le $r
do
while test $j -le $i
do
echo -n “ $ch ”
j=`expr $j + 1`
done
j=1
echo “ ”
i=`expr $i + 1`
done

OUTPUT :-

PROGRAM – 14
Objective : Write a program to check a number whether it is an Armstrong number or not.

CODE :

echo “ Enter a number to check ”

read n

temp=$n

s=0

while [ $n -ne 0 ]

do

r=`expr $n %10`

s=`expr $s + $r \* $r \* $r`

n=`expr $n / 10`

done

if test [ $s -eq $temp ]

then

echo “ It is an Armstrong number ”

else

echo “ Not an Armstrong number ”

fi

OUTPUT :-

PROGRAM – 15
Objective : To check whether the number is Palindrome or not.

CODE:

echo “Enter number”

read m

num=0

temp=$n

while [ $n -gt 0 ]

do

num=`expr $num \* 10`

k=`expr $n % 10`

num=`expr $sum +$k`

n=`expr $n / 10`

done

if [ $sum -eq $temp ]

then

echo “Number is Palindrome”

else

echo “Number is not Palindrome”

fi

OUTPUT :-
PROGRAM – 16

Objective: Write a Shell program to find the sum of square of individual digits of a number.

CODE:

echo “Enter a number:”

read n

t=$n

s=0

while [ $n -gt 0 ]

do

r=`expr $n % 10`

s=`expr $s + $r \* $r`

n=`expr $n / 10`

done

echo “The sum of square of individual digits of $t is $s”

OUTPUT :
PROGRAM – 17

Objective: Write a Shell program to find the smallest number from a set of numbers.

CODE:

echo “Enter a:”

read a

echo “Enter b:”

read b

echo “Enter c”

read c

if [ $a -le $b -a $a -le $c ]

then

echo “ a is smallest”

elif [ $b -le $c -a $b -le $a ]

then

echo “b is smallest”

else

echo “c is smallest”

fi

OUTPUT :-
PROGRAM – 18

Objective : Write a Shell program to find the smallest digit from a number.

CODE:

echo “Enter a number:”

read n

s=9

while [ $n -gt 0 ]

do

r=`expr $n % 10`

if [ $r -lt $s ]

then

s=$r

fi

n=`expr $n / 10`

done

echo “The smallest digit is : $s”

OUTPUT :-
PROGRAM – 19

Objective : Write a Shell program to show the get user input.

Code :
echo "Enter Your Name"
read name
echo "Welcome $name to LinuxHint"

Output :
PROGRAM – 20

Objective : Write a Shell program to show the use of AND logic.

Code :
echo "Enter username"
read username
echo "Enter password"
read password

if [[ ( $username == "admin" && $password == "secret" ) ]]; then


echo "valid user"
else
echo "invalid user"
fi

Output :
PROGRAM – 21

Objective : Write a Shell program to show the use of OR logic.

Code :
echo "Enter any number"
read n

if [[ ( $n -eq 15 || $n -eq 45 ) ]]
then
echo "You won the game"
else
echo "You lost the game"
fi

Output :
PROGRAM – 22

Objective : - Write a Shell program to show the use of case statement.

Code :
echo "Enter your lucky number"
read n
case $n in
101)
echo echo "You got 1st prize" ;;
510)
echo "You got 2nd prize" ;;
999)
echo "You got 3rd prize" ;;
*)
echo "Sorry, try for the next time" ;;
esac

Output :
PROGRAM – 23

Objective : Write a Shell program to combine string variables.

Code :
string1="Linux"
string2="Hint"
echo "$string1$string2"
string3=$string1+$string2
string3+=" is a good tutorial blog site"
echo $string3

Output :
PROGRAM – 24

Objective : Write a Shell program to pass return value from function.

Code :
function greeting() {

str="Hello, $name"
echo $str

echo "Enter your name"


read name

val=$(greeting)
echo "Return value of the function is $val"

Output :
PROGRAM – 25

Objective : Write a Shell program to create a directory.

Code :

echo "Enter directory name"


read newdir
`mkdir $newdir`

Output :
PROGRAM – 26

Objective : Write a Shell program to delete a file.

Code :
echo "Enter filename to remove"
read fn
rm -i $fn

Output :
PROGRAM – 27

Objective : Write a Shell program to append a file.

Code :
echo "Before appending the file"
cat book.txt

echo "Learning Laravel 5">> book.txt


echo "After appending the file"
cat book.txt

Output :
PROGRAM – 28

Objective : Write a Shell program to find the factorial of a number.

Code :
echo "Enter a Number"
read n
i=`expr $n - 1`
p=1
while [ $i -ge 1 ]
do
n=`expr $n \* $i`
i=`expr $i - 1`
done
echo "The Factorial of the given Number is $n"

Output :

ubuntu@ubuntu-VirtualBox:~/code$ bash factorial_number.sh


Enter the Number
5
The Factorial of the given Number is 120
PROGRAM – 29

Objective : Write a Shell program to find the swap two numbers.

Code :
echo "Enter Two Numbers"
read a b
temp=$a
a=$b
b=$temp
echo "after swapping"
echo $a $b

Output :

ubuntu@ubuntu-VirtualBox:~/code$ bash swap.sh


Enter two Numbers
23
after swapping
32
PROGRAM – 30

Objective : Writ a Shell program to count the no. of characters in a given string.

CODE :
clear
echo "Enter the string"
read str
len=`expr $str | wc -c`
len=`expr $len - 1`
echo "length of the ‘$str’ is $len"

Output :

ubuntu@ubuntu-VirtualBox:~/code$ bash count_char_in_string.sh


Enter the string
Hello World
Length of the ‘Hello World’ is 11

You might also like