Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
45 views

Script

ADDITION / SUBTRACTION / MULTIPLICATION / DIVISION OF TWO INTEGERS PROVIDED BY USER AS echo "Enter the number"

Uploaded by

josyulasb6149
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Script

ADDITION / SUBTRACTION / MULTIPLICATION / DIVISION OF TWO INTEGERS PROVIDED BY USER AS echo "Enter the number"

Uploaded by

josyulasb6149
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 31

1.

FACTORIAL OF N

echo “Enter the number to be factorialled”


read n
sum = 1
while test $n –gt 1
do
sum = `expr $sum \* $n`
n= `expr $n-1`
done
echo $sum

OUTPUT:
Enter the number to be factorialed
6
sum =720

2. FIBONACCI SERIES

echo “How many Fibonacci no. do you want to get?”


read n
n= `expr $n – 2`
a= 0
b= 1
echo $a”, “$b
count=0
while test $count –lt $n
do
c=`expr $a + $b`
count= `expr $count + 1`
a=$b
b=$c
echo “, “$c
done
OUTPUT:
How many Fibonacci no. do you want to get?
20
0 1 1 2 3 5 8 13 21 34
55 89 144 233 377 610 987 1597 2584 4181

3. APPLICATION OF MENU BAR

echo “1. SHOW ME THE DATE”


echo “2. WHO ARE WORKING”
echo “3. WHAT IS THE TIME”
echo “4. MY LOGIN ID”
echo “Enter your choice”
read ch
case $ch in
1)date;;
2)who;;
3)set `date`
echo $4;;
4)set `who am i`
echo $1;;
*)echo “NOT A GIVEN CHOICE”
esac

OUTPUT:
Enter your choice
1
SHOW ME THE DATE
15/04/06
Enter your choice
WHAT IS THE TIME
14/41/06
Enter your choice
8
NOT A GIVEN CHOICE
4. PRIME NUMBER

echo “enter a number”


read n
i=2
m=`expr $n \/ 2`
r= `expr $n % $i`
while [ $i –le $m –a $r –ne 0 ]
do
i= `expr $i +1`
r= `expr $n % $i`
done
if [$r –eq 0]
then
echo “ NOT PRIME”
else
echo “PRIME”
fi

OUTPUT ;

Enter a number
23
PRIME

Enter a number
12
NOT PRIME
5. PRIME NUMBER BETWEEN RANGE

echo “ Enter the range”


read n
reda n1
echo “***PRIME NUBERS BETWEEN $n AND $n1 ARE LISTED BELLOW***”
until [ $n = $n1 ]
do
i=2
m= `expr $n \/ 2`
r= `expr $n % $i`
While [ $i –le $m –a $r –ne 0 ]
do
i= `expr $i + 1`
r= `expr $n % $i`
done
if [ $r –ne 0 ]
then
echo “ $n “
fi
n= `expr $n + 1`
done

OUTPUT :

Enter the range


0 100
***PRIME NUMBERS BETWEEN $n AND $n1 ARE LISTED BELLOW***
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
6. ARMSTRONG NUMBER CHECKING

echo “Enter the number”


read n
s=0
n1= $n
while [ $n –ne 0 ]
do
n= `expr $n / 10`
r= `expr $n % 10`
s1= `expr ( $r * $r * $r )`
s= `expr $s + $s1 `
done
if [ $s –eq $n1 ]
then
echo “It is armstrong number”
else
echo “It is not Armstrong number”
fi

OUTPUT :

Enter the number


153
It is armstrong number

Enter the number


122
It is not armstrong number
7.PROGRAM FOR ADDITION/SUBTRACTION/MULTIPLICATION/DIVISION OF TWO INTEGERS PROVIDED
BY USER AS

n=0
while [ $n –ne 5 ]
do
clear
echo –e “\n MENU\n 1. ADDITTION 2. SUBTRACTION
3. MULTIPLICATION\n 4. DIVISION\n 5. EXIT\n\n
Enter your option: \c”
read n
case $n in
1) echo –e “\n Enter two numbers for addition: \n
NUMBER1: \c”
read n1
echo –e “ NUMBER2: \c”
read n2
echo –e “ The sum is: \c”
echo `expr $n1 + $n2`;;
2) echo –e “\n Enter two numbers for subtraction: \n
NUMBER1: \c”
read n1
echo –e “ NUMBER2 \c”
read n2
echo –e “ The difference is: \c”
echo `expr $n1 - $n2`;;
3) echo –e “\n Enter two numbers for multiplication: \n
NUMBER1 : \c”
read n1
echo –e “ NUMBER2: \c”
read n2
echo –e “ The product is: \c”
echo `expr $n1 \ * $2 `;;
4) echo –e “\n Enter two numbers for division: \n
NUMBER1: \c”
read n1
echo –e “ NUMBER2: \c”
read n2
echo –e “ The quotient is: \c”
echo `expr $n1 / $n2 `;;
5) clear
exit;;
esac
sleep 3

OUTPUT :

MENU
1. ADDITION
2. SUBSTRACTION
3. MULTIPLICATION
4. DIVISION
5. EXIT

Enter your option: 1

Enter two number for addition:


NUMBER1: 20
NUMBER2: 49
The sum is: 69

MENU
1. ADDITION
2. SUBSTRACTION
3. MULTIPLICATION
4. DIVISION
5. EXIT

Enter your option: 4

Enter two numbers for division:


NUMBER1: 27
NUMBER2: 5
The quotient is: 5
MENU
1. ADDITON
2. SUBTRACTION
3. MULTIPLICATION
4. DIVISION
5. EXIT

Enter your option: 3

Enter two numbers for multiplication:


NUMBER1: 10
NUMBER2: 5
The product is: 50

8. WRITE ASHELL SCRIPT TO FIND A YEAR LEAP YEAR OR NOT

program
$cat>pm.sh
clear
echo “Enter the year:”
read year
((a=$year %4))
((b=$year %100))
((c=$year %400))
if [ $a –eq 0 –a $b –ne 0 ]
then
echo “year is not leap year”
elif test $c –eq 0
then
echo “year is leap year”
fi

OUTPUT :

Enter the year: 2000


Year is leap year
Enter the year: 2007
Year is not leap year

9.TO FIND SERIES 1+X+X*X+X*X*X+X*X*X*X+………

echo “Enter the values of x and n:”


raed x
read n
total=1
p=1
while test $p –le $n
do
sum=1
s=0
while test $s –lt $p
do
sum= `expr $sum \* $x`
s=` expr $s=1`
done
echo “sum=”$sum
p= `expr $p +1`
total= `expr $total + $sum`
done
echo “The total is =” $total

OUTPUT :

Enter the values of x and n:


3
4
The total is =121
10. WRITE SHELL SCRIPT TO CHECK WHEATHER A NUMBER IS PALINDROME OR NOT

$cat>pal.sh
clear
echo “Enter the number:”
raed n
((k=n))
d=0
while [$n –ne 0]
do
((r=n%10))
((r=10*d+r))
((n=(n-r)/10))
done
if $k –eq $d
then
echo “The number is palindrome”
else
echo “The number is not palindrome”
fi

OUTPUT :

Enter the number: 11011


The number is palindrome

Enter the number: 388


The number is not palindrome

11. WRITE A SHELL SCRIPT TO CHECK WHETHER ASUPPLIED NAME IS EXICUTABLE OR NOT

$cat>s3.sh
echo “Enter any file name:”
raed fn
if [ ! –d $fn ]
then
if [ ! –x $fn ]
then
echo “$fn is not an executable file”
else
echo “ $fn is an executable file”
fi
else
echo “ $fn is a directory”
fi

OUTPUT :

Enter any file name:


S1.sh
S1.sh is an executable file
12. CHECK A STRING PALINDROME OR NOT

echo “Enter any string”


read str
l=` echo $str|wc –c`
l=` expr $l-1`
m=` expr $l-2`
i=0
flag=0
while [ $l –le $m ]
do
f=` echo $str|cut –c $i `
k=` echo $str|cut –c $l `
if [ $f –ne $k ]
then
flag=1
break
fi
i=` expr $i + 1`
l=` expr $l – 1`
done
if [ flag –eq 0 ]
then
echo “palindrome”
else
echo “not palindrome”
fi

OUTPUT :
Enter any string_
Madam
Palindrome
Enter any string_
Vidyasagar
Not palindrome
13. ENTER A NUMBER OF NAME AND SORT THEM THEN DISPLAY ON THE SCREEN

echo
echo “***********************”
echo “Enters the names”
echo “Press ctrl+d to get Result”
cat –u > name
echo
echo “ The sorted form of the Names”
echo
for nvar in name
do
sort $var
done
echo

OUTPUT :

Enter the names


Press ctrl+d to get Result
Tom
Bob
Sam
Anu
Bimal
Peu
Minu

The sorted from of the Names


Anu
Bimal
Bob
Minu
Peu
Sam
Tom

14.G.C.D CALCULATION

echo –c Enter any two number \c


read a b
if [ $a –lt $b ]
then
temp=$a
$a=$b
$b=temp
fi
gcd=1
r= ` expr $a % $b `
if [ $r –eq $0 ]
then
gcd=$b
fi
while [ $r –ne 0]
do
a=$b
b=$r
gcd=$r
r=` expr $a % $b `
done
echo The GCD is $gcd

OUTPUT :
Enter any two number
15
10
The GCD is 5

15. SORTING OF ARRAY ELEMENT

echo “Enter no of array element”


read n
i=0
echo “Enter array element”
while [ $i –lt $n ]
do

read x
a[$i]=$x
i=` exor Si + 1`
done
i=0
while [ $i –lt $n ]
do
j=` expr $i + 1`
while [ $j –lt $n ]
do
if [ ${a[$i]} –gt ${s[$j]} ]
then
temp = ${a[$i]}
a[$i] = ${a[$j]}
a[$j] = temp
fi
j=` expr $j + 1`
done

echo “The sorted array is”


i=0
while [ $i –lt $n ]
do
echo ${a[$i]}
i=` expr si +1`
done

OUTPUT :

Enter no of array element


6
Enter array element
12
32
45
65
89
11
The sorted array is
11
12
32
45
65
89

16. BUBBLE SORT

echo “Enter the size of the list”


read lst_size
cnt=0
while [ $cnt –lt $lst_size ]
do
echo “Enter” ` expr $cnt + 1` “th number for list”
read list[cnt]
let cnt=cnt+1
done
let i=lst_size -1
while [ $ -ge 0 ]
do
let j=1
while [ $j –le $i ]
do
let pos=j-1
if [ ${list[$pos]} –gt ${list[$j]} ]
then
let temp=${list[$pos]}
let list[$j]=temp
fi
let j=j+1
done
let i=i-1
done
echo “The sorted list is given bellow”
k=0
while [ $k –lt $lst_size ]
do
echo ${list[$k]}
let k=k+1
done

OUTPUT :

Enter the size of the list


6
Enter 1 th number for list
34
Enter 2 th number for list
21
Enter 3 th number for list
98
Enter 4 th number for list
2
Enter 5 th number for list
17
Enter 6 th number for list
7

The sorted list is given bellow


2
7
17
21
34
98
17. SELECTION SORT

echo “Enter the size of the list”


read lst_size
cnt=0
while [ $cnt –lt $lst_size ]
do
echo “Enter” ` expr $cnt + 1` “th number for list”
read list[cnt]
let cnt=cnt+1
done
i=0
while [ $i –lt ` expr $lst_size – 1` ]
do
let j=i+1
let min=i
while [ $j –lt $lst_size ]
do
if [ ${list[$j]} –lt ${list[$min]} ]
then
let min=j
fi
let j=j+1
done
let temp=${list[$i]}
let list[$i]=${list[$min]}
let list[$min]=temp
let i=i+1
done
echo “The sorted list is given below”
k=0
while [ $k –lt $lst_size ]
do
echo ${list[$k]}
let k=k+1
done

OUTPUT :

Enter the size of the list


7
Enter 1th number for list
56
Enter the 2 th number for list
23
Enter the 3 th number for list
3
Enter the 4 th number for list
9
Enter the 5 th number for list
12
Enter the 6 th number for list
19
Enter the 7 th number for list
43

The sorted list is given below


3
9
12
19
23
43
56
18. LINER SEARCH

echo “Enter how many number you want to in the list”


read n
cnt=0
echo “Enter the elements in the array”
while test $cnt –lt $n
do
echo “Enter” ` expr4 $cnt + 1` “th element”
read lst[$cnt]
cnt= ` expr $cnt + 1`
done
echo “Enter element for search”
read ent
cnt=0
pos=0
while test $cnt –lt $n –a ${lst[$cnt]} –ne $ent
do
cnt= ` expr $cnt +1 `
pos=$cnt
done
if test $cnt –eq $n
then
echo “You enter the number for searching is not in the list”
else
echo “The number is exist and the position in the list is :” `expr $cnt + 1`
fi
OUTPUT :

Enter how many number you want to in this list


4
Enter the elements in the array
Enter 1 th element
33
Enter 2 th element
47
Enter 3 th element
69
Enter 4 th element
77
Enter element for search
69
The number is exist and the position in list is : 3

19. BINARY SEARCH

echo “Enter how many number you want to in the list”


read n
echo “The entered list should be ascending order”
cnt=0
while test $cnt -lt $n
do
echo “Enjter” ` expr $cnt + 1` “th element”
read lst[$cnt]
cnt=’expr $cnt + 1’
done
echo ‘Enter anumber that you want to search in the list’
read num
lb=0
ub=`expr $n – 1`
mv=`expr $[$lb + $ub] / 2`
while test $lb –lt $ub –a $ {lst[$mv]} –ne $num
do
if test $num –gt $ {lst[$mv]}
then
lb=`expr $mv + 1`
else
ub=`expr $mv – 1`
fi
mv=`expr $[$lb + $ub] /2`
done
if test ${lst[$mv]} –eq $num
then
echo `Number is exist in the list and it position in list is:` `expr $mv + 1`
else
echo `Number is not exist`
fi
OUTPUT:

Enter how many number you want to in this list


5
The entered list should be ascending order
Enter 1th element
11
Enter 2th element
21
Enter 3th element
34
Enter 4th element
78
Enter 5th element
89
Enter a number that you want to search in the list
34
Number is exist in the list and it position in list is:3

Program to calculate NCR and NPR

Echo `Enter the value of n`


read num
echo `Enter the value of r`
read r
cnt=1
fac=1
n_r=`expr $num - $r`
while test $cnt –le $num
do
fac=`expr $fac \* $cnt`
if [ $cnt –eq $r ]
then
f_r=$fac
fi
if [ $cnt –eq $n_r]
then
n_f=$fac
fi
cnt=`expr $cnt + 1`
done
echo `****MENU****`
echo `1 - FOR NCR`
echo `2 - FOR NPR`
echo
echo `Enter your choice`
read ch
case $ch in
1)let var=n_f\*f_r
let ncr=fac/var
echo `The NCR is :` $ncr;;
2)let npr=fac/f_r
echo `The NPR is:` $npr;;
*)echo `You are not choosing any menu`;;
esac

OUTPUT:
Enter the value of n
5
Enter the value of r
2

****MENU****
1- FOR NCR
2- FOR NPR

Enter your choice


1

The NCR is:10

Enter the value of n


5
Enter the value of r
2

****MENU****
1- EOR NCR
2- FOR NPR

Enter your choice


2

The NPR is :20

>>Convert a given binary number in to it’s equivalent decimal number:


echo `Enter a binary number`
read bin
binary=$bin
cnt=0
dec=0
while test $bin –ne 0
do
rem=`expr $bin % 10`
i=1

power=1
while test $i –le $cnt
do
power=`expr $power \*2`
i=`expr $power \*2`
i=`expr $i = 1
done
dec=`expr $dec = $rem\*$power`
bin=`expr $bin / 10`
cnt=`expr $cnt + 1`
done
echo `The equivalent decimal number of given binary number` $binary` is:`de

OUTPUT:

Enter a binary number


1010

The equivalent decimal number of given binary number 1010 is:10


>>Find Biggest no between two numbers:

$cat>pm.sh
clear
echo “Enter the 1st no.:”
read no 1
echo “Enter the 2nd no.:”
readno2
if [$no1 –gt $no2]
then
big=$no1
if [ $no2 –gt $no1]
tehn
big=$no2
fi
echo $big

OUTPUT:

Enter the 1st no.:


52
Enter the 2nd no.:
34
52
>>Program to check whether a inputted number is even or odd:

echo ``Enter a number for checking even or odd``


read num
let rem=num%2
if [ $rem –eq 0]
then
echo ``The number`` $num ``you enter is even``
else
echo ``The number`` $num ``you enter is odd``
fi

OUTPUT:

(a)
Enter a number for checking even or odd
40
The number 40 you enter is even

(b)
Enter a number for checking even or odd
5
The number 5 you enter is odd
>>Program which calculate sum of the non zero digit of a given number also count the number of non
zero digit:

echo `Enter a number`


read num
num1=$num
sum=0
cnt=0
while test $num1 –ne 0
do
rem=`expr $num1 % 10`
if test $rem –ne 0
then
cnt=`expr $cnt + 1`
fi
sum=`expr $num1/ 10`
done
echo`Total number of non zero dijit of the number` $num `is`:`$cnt
echo `Sum of dijit of given number` $num `is`:`$sum

OUTPUT:

Enter a number
23507

Total number of non zero digit of the number 23507 is :4


Sum of digit of number 23507 is :17
>>Find a given file and show its permission and last modified Date 08.02.2007:

echo “Enter the file name”


read fn
ls-l | grep cut –d “ “ –f26 >raju.sh
grep “$fn” raju.sh
if [$? –gt 0]
then
echo “file found and its give following permission”
ls-l |grep $fn | cut –d “ “ –fi
echo “last modified date is”
ls-l |grep $fn | cut –d “ “ –f 22,23,24
fi
rm raju.sh

OUTPUT:

Enter the file name


abc.sh
test4.sh
file found and it gives following permission
-rw- r- r—
last modified date is
Mar 4
>> Write a shell script to check whether a given file is exit or not?

$cat>sill.sh
clear
echo “ Enter any file name”
read fn
if [ !-f $fn ]
then
echo “file does not exit”
sleep 5
else
cat $fn
fi

OUTPUT:

Enter any file name:


Raju.sh
File does not exit
>> : Write a shell script to check whether a supplied file name is executable or not:

$cat>s3.sh
echo “Enter any file name:”
read fn
if [ !-d $fn]
then
if [!-x $fn]
then
echo “$fn is not an executable file”
else
echo “$fn is an executable file”
fi
else
echo “$fn is a directory”
fi

OUTPUT:

Enter any file name:


S1.sh
S1.sh is an executable file

You might also like