Saturday 23 May 2020

Shell Script to Delete All Even Numbered Lines from a Text File



Program
echo "enter a file name"
read fname
lc=0
if test -f $fname
then
   while read line
   do
      lc=$((lc+1))
      even=$((lc%2))
      if test $even -ne 0
      then
         echo $line>>newfile.txt
      fi
   done < $fname
   rm $fname
   mv newfile.txt $fname
else
   echo "$fname file not exist"
fi


OUTPUT
csc@csc-SVE1513CYNB ~ $ cp avg ttavg
csc@csc-SVE1513CYNB ~ $ bash avg
enter a file name
ttavg


echo "enter a file name"
lc=0
then
do
even=$((lc%2))
then
fi
rm $fname
cat $fname
echo "$fname file not exist"

Shell script to accept student test marks and find the result

Write a shell script to accept student information like name, class and 3 subject marks. Calculate the total and average. Result will be displayed based on average. If avg >=90 (Distinction), avg >=60 and avg<90 (First class), avg>=35 and avg<=59(Second class), avg <35 (Fail).


Program
echo "enter name"
read name
echo "enter class"
read class
echo "enter marks of 3 subjects"
read a
read b
read c
total=$(($a+$b+$c))
avg=$(($total/3))
echo "Total:$total"
echo "Average:$avg"
echo -n "Result:"
if test $avg -ge 80 -a $avg -le 90
then
   echo "Distinction"
elif test $avg -ge 60 -a $avg -lt 80
then
   echo "First class"
elif test $avg -ge 35 -a $avg -le 59
then
   echo "Second class"
else
   echo "Fail"
fi

OUTPUT
csc@csc-SVE1513CYNB ~ $ bash avg
enter name
martin
enter class
bsc
enter marks of 3 subjects
45
54
55
Total:154
Average:51
Result:Second class

Friday 15 May 2020

Shell script to count the number of lines in a file which does not contain a vowel.

PROGRAM

echo "Enter the file"
read fname
ans=`echo grep -v '[aeiou]' $fname|wc -l`

echo "Number of lines in $fname which does not contain a vowel is $ans."

OUTPUT
Enter the file
vw
Number of lines in vw which does not contain a vowel is 1.