Saturday 6 April 2019

Shell script to read in two numbers and display the number divisible by the first number in the given range

Program
echo "Enter two numbers"
read m n
echo "Numbers divisible by [$m] in the range [$m] to [$n]"
for((i=$m;i<=$n;i++))
do
    div=`expr $i % $m`
    if test $div -eq 0
    then
         echo $i
    fi
done



OUTPUT
Safi $ bash samp.sh
Enter two numbers
3 20
Numbers divisible by [3] in the range [3] to [20]
3
6
9
12
15
18

Count number of files and sub-directories in a given directory

Shell script to input a directory name and display the number of files and
sub directories present in the given directory.


Program
echo -n "Enter directory name: "
read dr
dircnt=`ls -l $dr|grep "^d"|wc -l`
filecnt=`ls -l $dr|grep "^-"|wc -l`
echo "No of directories = $dircnt"
echo "No of files = $filecnt"


OUTPUT
Safi $ sh dircnt.sh
Enter directory name: .
No of directories = 10
No of files = 98


Safi $ sh dircnt.sh
Enter directory name: sasha
No of directories = 0
No of files = 4

Friday 5 April 2019

Shell script that asks for a word and a file name and then tells how many lines contain that word.

echo "Enter word,filename";
 read word filename
n=`grep -c "$word" $filename`;
echo "The word [$word] in the $filename has occurred in $n lines";




OUTPUT
Safi>sh eb.sh
Enter word,filename
word eb.sh
The word word in the eb.sh has occured in 4 lines

Safi>sh eb.sh
Enter word,filename
for eb.sh
The word for in the eb.sh has occured in 0 lines

Thursday 4 April 2019

Shell script to read a username and display how many times a user has logged in.

#Counting the number of times a user has logged in
echo "Enter the username"
read uname
times=`who | grep  "$uname" | wc -l`
if [ $times -ne 0 ]
then
   echo "$uname has logged in $times times"
else
   echo "user [$uname] not logged in"
fi

 
OUTPUT
Safi>sh eb.sh
Enter the username
csc
csc has logged in 3 times
Safi>sh eb.sh
Enter the username
sss
user [sss] not logged in

Shell script to perform linear search in an array

#Check whether a number is present or not
echo -n "Enter limit n: "
read n
for((i=0;i<n;i++))
do
 read a[$i]
done
echo ${a[*]}
echo -n "Enter number to be searched: "
read key
pos=0
for((i=0;i<n;i++))
do
  if test $key -eq ${a[$i]}
  then
      pos=`expr $i + 1`
      break;
  fi
done
if test $i -ge $n
then
  echo "Number not found"
else
  echo "Number [$key] found at position $pos"
fi





OUTPUT
Safi>bash numsrch.sh
Enter limit n: 5
7
6
8
5
9
7 6 8 5 9
Enter number to be searched: 7
Number [7] found at position 1

Shell script to count the vowels in the given string

#script to count the number of vowels in the given string using if
echo Enter string
read str
count=0
len=`echo $str | wc -c`
len=`expr $len - 1`
while test $len -gt 0
do
     temp=`echo $str | cut -c $len | tr [A-Z]  [a-z]`
     len=`expr $len - 1`
     if test  "$temp" = "a" -o "$temp" = "e" -o "$temp" = "i" -o
            "$temp" ="o"  -o  "$temp" = "u"
     then
                           echo “$temp”
                           count=`expr $count + 1`
     fi
done
echo Number of vowels in the given string \"$str\" are "$count"
 


OUTPUT
Safi> sh vwlcnt.sh
Enter string
good
“o”
“o”
Number of vowels in the given string "good" are 2