Home > GE2155 computer lab > CP Lab II Program

CP Lab II Program

Display the files starting with the alphabet
echo ” enter the alphbet”
read ch
find -name “$ch*” -print > tmp
if [ -s tmp ]
then
echo ” the files starting with $ch are”
cat tmp
else
echo ” there are no files starting with $ch”
fi
rm tmp

find xn
echo -n “enter the value of x”
read x
echo -n ” enter the power of x”
read n
sum=1
i=1
while [ $i -le $n ]
do
sum=`expr $sum \* $x`
i=`expr $i + 1`
done
echo “the value of $x power $n is $sum”

Find the Class of the Character
if test $# -ne 1
then
echo ” enter the character data”
exit 1
fi
char=$1
case “$char” in
[0-9] ) echo ” the given arg is digit :$char”;;
[a-z] ) echo ” the given arg is lowercase letter $char”;;
[A-Z] ) echo ” the given arg is uppercase letter $char”;;
? ) echo ” the given arg is special character $char”;;
* ) echo ” enter a single character”;;
esac

Copy one string to another string
echo -n “enter the stirng”
read as
l=`echo $as | wc -c`
i=1
while [ $i -le $l ]
do
es2=`echo $as | cut -c $i`
i=`expr $i + 1`
cs3=`echo $cs3$es2`
done
echo ” the actual string is $as”
echo ” the copied string is $cs3″

Count the number of digit
echo -n ” enter the number”
read n
sd=0
while [ $n -gt 0 ]
do
a=`expr $n % 10`
sd=`expr $sd + 1`
n=`expr $n / 10`
done
echo ” the total number of digits in the given number is $sd”

Calculate the HRA
echo -n ” enter employee’s basic salary”
read bsal
if [ $bsal -gt 5000 ]
then
hra=`expr $bsal / 5`
echo “HRA = $hra”
elif [ $bsal -ge 4000 -a $bsal -le 5000 ]
then
hra=`expr $bsal / 7`
echo “HRA = $hra”
else
hra=`expr $bsal / 10`
echo “HRA = $hra”
fi

Find the Length of the String
echo -n “enter the string”
read string
l=`expr $string | wc -c`
l=`expr $l – 1`
echo ” the length of the given string $string is $l”

Check the Login
echo ” enter your login name ”
read lname
if [ $LOGNAME = $lname ]
then
echo “your login name is $lname”
else
echo ” your login name is not $lname current login name is $LOGNAME”
fi

Occurrence of the Character in a String
echo -n “enter the string:”
read s1
echo -n ” enter the character to be searched”
read c1
l=`echo $s1 | wc -c`
l=`expr $l – 1`
c=0
while [ $l -gt 0 ]
do
t=`echo $s1 | cut -c $l`
if [ $t = $c1 ]
then
c=`expr $c + 1`
fi
l=`expr $l – 1`
done
echo ” the total number of occurrence of $c1 char in the given stirng $s1 is $c”

Display the Owner of the File
echo -n ” enter the file name”
read name
ls -l > tmp
if (grep $name tmp > tmp1)
then
line=`cat tmp1 | tr -s ” ” | cut -d” ” -f3`
##echo “line:$line”
fi
check=`echo $line | wc -c`
echo $check
if test $check -gt 1
then
echo ” the owner of the file $name is $line”
else
echo “the file $name doest exists”
fi

Change the file permission
echo “enter the file name”
read fname
ls -l $fname
echo “do you want to assign read,write and execute for particular file[ y/n]”
read ch
if [ $ch = y ]
then
chmod 777 $fname
echo “permissions are given”
ls -l $fname
else
echo “permission are not given”
fi
Replace the character in a string
echo -n “enter the string”
read s1
echo -n ” enter the char to be replaced”
read ocr
echo -n ” enter the new char for replacement”
read ncr
l=`echo $s1 | wc -c`
l=`expr $l – 1`
c=0
j=1
while [ $j -le $l ]
do
t=`echo $s1 | cut -c $j`
if [ $t = $ocr ]
then
ns=`echo $ns$ncr`
else
ns=`echo $ns$t`
fi
j=`expr $j + 1`
done
echo ” the old string is $s1″
echo ” the new string is $ns”
Reverse the given number
echo ” enter the number”
read n
rn=0
while [ $n -gt 0 ]
do
a=`expr $n % 10`
rn=`expr $rn ‘*’ 10 + $a`
n=`expr $n / 10`
done
echo ” the reversed given number is $rn”

Calculate the Simple interest
echo -n “enter the principal amount : Rs”
read p
echo -n “enter the number of years”
read n
echo -n “enter the rate of interest”
read r
si=`expr “scale=2; ($p * $n * $r) / 100” | bc`
echo “simple interest = Rs.$si”

Find square and cube
echo ” enter the value”
read a
sq=`expr $a \* $a`
cu=`expr $sq \* $a`
echo ” square of $a is $sq”
echo ” cube of $a is $cu”

STUDENT DETAILS

echo STUDENT DETAILS
echo enter the name
read name
echo enter the roll number
read rno
echo enter the marks of 3 subjects
read m1 m2 m3
total=`expr $m1 + $m2 + $m3`
avg=`expr $total / 3`
echo NAME $name
echo ROLL NUMBER $rno
echo MARK1 $m1
echo MARK2 $m2
echo MARK3 $m3
echo TOTAL $total
echo AVERAGE $avg
if [ $avg -ge 90 ]
then
echo GRADE A
elif [ $avg -ge 80 ]
then
echo GRADE B
elif [ $avg -ge 70 ]
then
echo GRADE C
elif [ $avg -ge 60 ]
then
echo GRADE D
else
echo GRADE E
fi

OUTPUT

enter the name
A
enter the roll number
10
enter the marks of 3 subjects
60 70 80
NAME A
ROLL NUMBER 10
MARK1 60
MARK2 70
MARK3 80
TOTAL 210
AVERAGE 70
GRADE C

TO FIND SUM OF FIRST N NATURAL NUMBER

echo enter the value for n
read n

i=1
sum=0
until [ $i -gt $n ]
do
sum=`expr $sum + $i`
i=`expr $i + 1`
done
echo sum of $n natural number $sum

OUTPUT

enter the value for n
4
sum of 4 natural number 10

TO FIND NUMBER OF VOWELS IN GIVEN STRING

echo enter the string
read s1
l=`echo $s1 | wc -c`
l=`expr $l – 1`
v=0
while [ $l -gt 0 ]
do
t=`echo $s1 | cut -c $l`
if [ $t = a ]
then
v=`expr $v + 1`
elif [ $t = e ]
then
v=`expr $v + 1`
elif [ $t = i ]
then
v=`expr $v + 1`
elif [ $t = o ]
then
v=`expr $v + 1`
elif [ $t = u ]
then
v=`expr $v + 1`
fi

OUTPUT

enter the string
engineer
the total vowels 4

SUM OF DIGITS OF A NUMBER

echo ENTER THE NUMBER
read a
sum=0
i=0
while [ $a -ne 0 ]
do
i=`expr $a % 10`
a=`expr $a / 10`
sum=`expr $sum + $i`
done
echo THE SUM OF THE DIGITS IS $sum

SAMPLE OUTPUT

ENTER THE NUMBER
45
THE SUM OF THE DIGITS IS 9

SUM AND PRODUCT OF TWO NUMBERS

Echo enter the number
read a b
c = ` expr $a + $b`
d = `expr $a \* $b`
echo the sum is : $c
echo the product is: $d

OUTPUT

enter the number 2 4
the sum is : 6
the product is: 8

String operation

PROGRAM
echo ENTER THE CHOICE
echo 1.STRING LENGTH
echo 2.STRING CONCATENATION
echo 3.STRING COMPARISION
read ch
case $ch in
1)c=$1 | wc -c
c=`expr $1 -1`
echo LENGTH OF $1 IS $c;;
2)c=`expr$1$2`
echo CONCATENATED STRING IS $c;;
3)if [ $1 -eq $2 ]
then
echo STRINGS ARE EQUAL
else
echo STRINGS ARE NOT EQUAL
fi;;
*)echo WRONG CHOICE
Esac

OUTPUT
[eeepss08@eeelinum]$sh string.sh super man
ENTER THE CHOICE
1.STRING LENGTH
2.STRING CONCATENATION
3.STRING COMPARISON
2
THE CONCATENATED STRING IS SUPERMAN

STRING PALINDROME

echo Enter a string
read str
l=`expr $str | wc -c`
l=`expr $l – 1`
while [ $l -gt 0 ]
do
s=`expr $str | cut -c $l`
new=`expr $new$s`
l=`expr $l – 1`
done

if [ $new = $str ]
then
echo It is a Palindrome

else
echo It is not a Palindrome

fi

SAMPLE OUTPUT

Enter a string
refer
It is a Palindrome

FACTORIAL OF A GIVEN NUMBER

echo enter the limit
read a
f=0
s=1
t=`expr $f + $s`
echo the fibonacci series is
echo $f
echo $s
i=1
while [ $i -le $a ]
do
echo $t
f=$s
s=$t
t=`expr $f + $s`
i=`expr $i + 1`
done

OUTPUT

enter the limit
5
the fibonacci series is
0
1
1
2
3
5
8

FACTORIAL OF A GIVEN NUMBER

echo enter the number
read a
fact=1
i=1
while [ $i -le $a ]
do
fact=`expr $fact \* $i`
i=`expr $i + 1`
done
echo the factorial of $a is $fact

OUTPUT

enter the number
5
the factorial of 5 is 120

COUNTING VOWELS

echo enter the string
read str
l=`expr $str | wc -c`
l=`expr $l – 1`
while [ $l -gt 0 ]
do
s=`expr $str | cut -c$l`
if [ $s = a -o $s = A ]
then
count=`expr $count + 1`

elif [ $s = e -o $s = E ]
then
count=`expr $count + 1`

elif [ $s = i ]
then
count=`expr $count + 1`

elif [ $s = o ]
then
count=`expr $count + 1`

elif [ $s = u ]
then
count=`expr $count + 1`
fi

l=`expr $l – 1`
done

if [ $count -eq 0 ]
then
echo No Vowel
else
echo $count
fi

ARITHMETIC OPERATIONS USING SWITCH CASE

h=1
while [ $h -eq 1 ]
do
echo enter two numbers
read a b
echo enter your choice
echo 1.add 2.subtract 3.multiply 4.divide
read ch
case $ch in
1)c=`expr $a + $b`
echo the sum is $c;;
2)c=`expr $a – $b`
echo the difference is $c;;
3)c=`expr $a \* $b`
echo the product is $c;;
4)c=`expr $a / $b`
echo the quotient is $c;;
echo wrong choice entered;;

echo enter 1 to continue
read h
done

OUTPUT

enter two numbers
3 3
enter your choice
1.add 2.subtract 3.multiply 4.divide
1
the sum is 6
enter 1 to continue
1
enter two numbers
4 2
enter your choice
1.add 2.subtract 3.multiply 4.divide
3
the product is 8
enter 1 to continue
2

Categories: GE2155 computer lab
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: