Вы находитесь на странице: 1из 24

Roll no.

2208309

Program no. 1
AIM : Script to check whether a year is leap or not
#!/bin/bash
yr=$1
echo "The year to be checked for leap year is $1"
c=`expr $yr \% 4`
if [ $c -eq 0 ] ; then
echo "The year $1 is a leap year"
else
echo "The year $1 is not a leap year"
fi

Roll no. 2208309

OUTPUT:

Roll no. 2208309

Program no. 2
AIM : Script to print a table of a given number
#!/bin/bash
no=$1
echo -e "\nThe number for which table is to be printed is $1"
for (( i=0 ; i<=10 ; i++ ))
do
c=`expr $1 \* $i`
echo -e "\n $1 * $i = $c"
done

Roll no. 2208309

OUTPUT:

Roll no. 2208309

Program no.3
AIM : Script to check whether number is even or odd
#!/bin/bash
no=$1
echo "The number to be checked for even odd is $1"
c=`expr $no \% 2`
if [ $c -eq 0 ] ; then
echo "The number $1 is a even"
else
echo "The number $1 is odd"
fi

Roll no. 2208309

OUTPUT:

Roll no. 2208309

Program no. 4
AIM : Script to print natural number in reverse order
#!/bin/bash
no=$1
echo "The number to be checked for even odd is $1"
c=`expr $no \% 2`
if [ $c -eq 0 ] ; then
echo "The number $1 is a even"
else
echo "The number $1 is odd"
fi

Roll no. 2208309

OUTPUT:

Roll no. 2208309

Program no. 5
AIM : Script to find sum of first n natural numbers
#!/bin/bash
echo "Enter the natural number till where sum is to be calculated"
read no
a=`expr 1 \+ $no`
c=`expr $a \* $no`
b=`expr $c \/ 2`
echo "Sum of first $no natural number is $b"

Roll no. 2208309

OUTPUT:

10

Roll no. 2208309

Program no. 6
AIM : Script to find factorial of given number

#!/bin/bash
echo "Enter the no for which factorial is to be searched"
read no
a=$no
for (( i=`expr $no \- 1` ; i>1 ; i-- ))
do
a=`expr $a \* $i`
done
echo "Factorial of $no is $a"

11

Roll no. 2208309

OUTPUT:

12

Roll no. 2208309

Program no.7
AIM : Script to find largest of three numbers
#!/bin/bash
echo "To check which is the largest number"
echo -n "Enter the 1st number "
read no1
echo -n "Enter the 2nd number "
read no2
echo -n "Enter the 3rd number "
read no3
if [ $no1 -gt $no2 ] ; then
if [ $no1 -gt $no3 ] ; then
echo "Greatest number is $no1"
else
echo "Greatest number is $no3"
fi
else
if [ $no2 -gt $no3 ] ; then
echo "Greatest number is $no2"
else
echo "Greatest number is $no3"
fi
fi

13

Roll no. 2208309

OUTPUT:

14

Roll no. 2208309

Program no. 8
AIM : Script to find fibonacci series
#!/bin/bash
echo "How many fibonacci numbers do u want "
read limit
a=0
b=1
d=1
echo -n "$a "
while [ $d -lt $limit ]
do
c=`expr $a \+ $b`
echo -n "$c "
b=$a
a=$c
d=`expr $d \+ 1`
done
echo " "

15

Roll no. 2208309

OUTPUT:

16

Roll no. 2208309

Program no. 9
AIM : Script to add, subtract, multiply and divide 2 numbers using case
#!/bin/bash
echo "To add, sub, multiply and devide using case"
echo "Enter the first no "
read no1
echo "Enter the second no "
read no2
echo -e "\nFor Addition 1"
echo -e "\nFor Subtraction 2"
echo -e "\nFor Multiplication 3"
echo -e "\nFor Division 4"
read option
case $option in
1)
echo "Starting addtion"
c=`expr $no1 \+ $no2`
echo "$no1 + $no2 = $c"
;;
2)
echo "Starting subtraction"
c=`expr $no1 \- $no2`
echo "$no1 - $no2 = $c"
;;
3)
echo "Starting multiplication"
c=`expr $no1 \* $no2`
echo "$no1 * $no2 = $c"
;;
4)
echo "Starting division"
if [ $no2 -eq 0 ] ; then
echo "Division is not possible since number2 is 0"
else
c=`expr $no1 \/ $no2`
echo "$no1 / $no2 = $c"
fi
;;
*)
echo "Enter the correct option"
exit 1
;;
esac
17

Roll no. 2208309

OUTPUT:

18

Roll no. 2208309

Program no. 10
AIM : Script to check whether a string is palindrome or not
#!/bin/bash
echo -n "Enter the name "
read name
len=`echo -n $name | wc -c`
echo "Length of the name is "$len
while [ $len -gt 0 ]
do
rev=$rev`echo $name | cut -c $len`
len=`expr $len \- 1`
done
echo "Reverse of the name is "$rev
if [ $name = $rev ]
then echo "It is a Palindrome"
else
echo "It is not a Palindrome"
fi

19

Roll no. 2208309

OUTPUT:

20

Roll no. 2208309

Program no. 11
AIM : Script to convert contents of file from lower to upper case and vice versa
#!/bin/bash
echo "Script to convert lower to upper and upper to lower"
echo "To convert to upper to lower 1"
echo "To convert to lower to upper 2"
read s
case $s in
1)
echo "Enter the file to convert to lower case"
read file
cat $file |tr 'A-Z' 'a-z'
;;
2)
echo "Enter the file to convert to upper case"
read file
cat $file |tr 'a-z' 'A-Z'
;;
*)
echo "The entered option is incorrect"
;;
esac

21

Roll no. 2208309

OUTPUT:

22

Roll no. 2208309

Program no. 12
AIM : Script to check whether the number is prime or not
#!/bin/bash
echo -n "Enter the number to check wheather it is a prime or not "
read no
#c=`expr $no \/ 2`
c=`expr $no \- 1`
j=2
while [ $j -lt $no ]
do
d=`expr $no \% $j`
if [ $d -eq 0 ] ; then
echo "Number is not Prime "
exit 0
else
j=`expr $j \+ 1`
fi
done
echo "Number is Prime "

23

Roll no. 2208309

OUTPUT:

24

Вам также может понравиться