Tuesday 28 July 2020

Shell script to find the total size of the files given as positional parameters using function


PROGRAM
#size function to find the total size of the files
size()
{
  s=0
  for i in $*
  do
    if test -f $i
    then
       sz=`ls -l $i|tr -s " "|cut -d " " -f5`
       s=`expr $s + $sz`
    else
       echo $i is not a file
    fi
  done
  return $s
}
size $*
ret=$?
echo "Total size is " $s



OUTPUT
sh sizFunc.sh fibt file fil2
fil2 is not a file
Total size is  159

Shell script to find the LCM and GCD of two numbers


PROGRAM
echo enter two numbers
read m n
echo "The given numbers are \c"
echo $m and $n
temp=`expr $m \* $n`
while [ $m -ne $n ]
do
    if [ $m -gt $n ]
    then
        m=`expr $m - $n`
    else
        n=`expr $n - $m`
    fi
done
echo GCD = $n
lcm=`expr $temp / $n`
echo LCM = $lcm



OUTPUT
enter two numbers
24 60
The given numbers are 24 and 60
GCD = 12
LCM = 120

Sunday 26 July 2020

Shell script to convert a decimal number to hexadecimal number



PROGRAM

echo "Enter a decimal number"
read n
num=$n
while test $n -gt 0
do
  d=`expr $n % 16`
  case $d in
      10) d='A' ;;
      11) d='B' ;;
      12) d='C' ;;
      13) d='D' ;;
      14) d='E' ;;
      15) d='F' ;;
  esac
  res=$res$d
  n=`expr $n / 16`
done
cnt=`echo $res|wc -c`
while test $cnt -gt 0
do
  c=`echo $res|cut -c $cnt`
  hex=$hex$c
  cnt=`expr $cnt - 1`
done
echo "($num) decimal = ($hex) hexadecimal"



OUTPUT
Enter a decimal number
10456
(10456) decimal = (28D8) hexadecimal

Monday 20 July 2020

Shell script to count the number of lines in a file that do not contain vowels.


PROGRAM
echo "Enter a filename"
read file
grep -v '^$' $file > gfl
lc=`grep -i -v -c [aeiou] gfl`
echo "Number of lines that do not contain any   vowels are: $lc"


OUTPUT
Enter a filename
fibt
Number of lines that do not contain any   vowels are: 0

Enter a filename
file3
Number of lines that do not contain any   vowels are: 3

Shell script to count the number of files whose size exceeds 100 bytes.


Program


c=0
for i in *
do
     if test -f $i
     then
        s=`ls -l $i | tr -s " " | cut -d " " -f5`
        if test $s -gt 100
        then
           c=`expr $c + 1`
        fi
     fi
done
echo "Number of files whose size exceeds 100 bytes is:$c"




OUTPUT
Number of files whose size exceeds 100 bytes is:91

Shell script to accept a string from the terminal and echo a suitable message if it doesn’t have at least 10 characters.


Program
echo "enter the string"
read str
Len=`expr  "$str" :  '.*'`
if test $Len -lt 10
then
     echo " $str has less than 10 characters"
else
echo  " the input string is \"$str\" "
fi


OUTPUT
enter the string
welcome to the world of UNIX
 the input string is "welcome to the world of UNIX"

enter the string
jhhj
 jhhj has less than 10 characters

Shell script to print n Fibonacci numbers


Program
echo "enter the number"
read n
echo "The Fibonacci numbers are"
f1=0
f2=1
echo "$f1"
echo "$f2"
n=`expr $n - 2`
while test $n -gt 0
do
   f3=`expr $f1 + $f2`
   echo "$f3"
   n=`expr $n - 1`
   f1=$f2
   f2=$f3
done



OUTPUT
enter the number
10
The Fibonacci numbers are
0
1
1
2
3
5
8
13
21
34