Friday, 20 April 2018

Shell script to change content into upper case

#Script to change the file name and contents into upper case
echo "enter the filename"
read fname
echo "File name changed to upper case"
if [ -f $fname ]
then
  echo $fname |tr '[a-z]' '[A-Z]'
fi

echo "\nOriginal file content"

cat $fname
echo "\nFile content in upper case"
tr '[a-z]' '[A-Z]' <$fname



OUTPUT
Safi>sh trans.sh
enter the filename
file2
File name changed to upper case
FILE2

Original file content
103 Banu CIO
103 Banu CIO
103 Banu CIO
104 Shan CFO
101 Sasha MD

File content in upper case
103 BANU CIO
103 BANU CIO
103 BANU CIO
104 SHAN CFO
101 SASHA MD

Thursday, 19 April 2018

Script to reverse the positional parameter list

#shell script to reverse print the positional parameter list
n=$#
for((i=n-1;i>=0;i--))
do
  arr[$i]=$1
  shift
done

echo ${arr[*]}



OUTPUT

Safi>bash revparam.sh 2 4 6 1 5 7 8 9 10
10 9 8 7 5 1 6 4 2

Monday, 9 April 2018

Shell script to find the ncr value

#Script to find the ncr value
echo "enter values of n and r"
read n
read r
t=0 i=1
a=1 b=1
c=1 d=0
while [ $i -le $n ]
do
a=`expr $a \* $i`
i=`expr $i + 1`
done
i=1
while [ $i -le $r ]
do
b=`expr $b \* $i`
i=`expr $i + 1`
done
i=1
d=`expr $n - $r`
while [ $i -le $d ]
do
c=`expr $c \* $i`
i=`expr $i + 1`
done
i=`expr $b \* $c`
t=`expr $a / $i`
echo "result=$t"



OUTPUT
Safi>sh ncr.sh
enter values of n and r
5
3
result=10

Shell script to display file attributes

#Script to extract and display the file attributes
echo "Enter the file name"
read f1
if [ -e $f1 ]
then
ap=`ls -l $f1 | cut -c2-10`
fn=`ls -l $f1 | tr -s " " | cut -d " " -f9`
lmt=`ls -l $f1 | tr -s " " | cut -d " " -f6,7,8`
sof=`ls -l $f1 | tr -s " " | cut -d " " -f5`
nol=`ls -l $f1 | tr -s " " | cut -d " " -f2`
un=`ls -l $f1 | tr -s " " | cut -d " " -f3`
fi
echo "The access permission of the given file is: $ap"
echo "The filename of the given file is: $fn"
echo "The last modification time of the given file is: $lmt"
echo "The size of given file is: $sof"
echo "The number of links of the given file: $nol"
echo "The username of the given file is: $un"



Safi>sh fchar.sh
Enter the file name
fchar.sh
The access permission of the given file is: rw-r--r--
The filename of the given file is: fchar.sh
The last modification time of the given file is: Apr 9 21:43
The size of given file is: 588
The number of links of the given file: 1
The username of the given file is: csc

Shell script to remove multiple copies of the same file content

#Script to compare contents of two files
echo "Enter two file name"
read f1 f2
d=`cmp $f1 $f2`
if [ -z "$d" ]
then
echo "Both file contents are same"
echo "File $f2 is deleted"
rm $f2
else
echo "Both are different"
fi





OUTPUT
Safi>sh cmpdel.sh
Enter two file name
ff file
Both are different

Safi>sh cmpdel.sh
Enter two file name
ff ff
Both file contents are same
File ff is deleted

Shell script to count the number of lines that does not contain a given word

#script to count the lines that does not contain a given word
echo enter file name and word
read file word
line=`grep -v "$word" $file| wc -l`
echo "$line lines does not contain word"



OUTPUT
Safi>sh notpres.sh
enter filename and word
notpres.sh echo
3 lines does not contain word

Shell script to display a multiplication table

#Script to display a multiplication table n times
echo "Enter the value for m and n"
read m n
for((i=1;i<=n;i++))
do
 echo $i '*' $m = `expr $i \* $m`
done



OUTPUT
Safi>bash mult.sh
Enter the value for m and n
5 10
1 * 5 = 5
2 * 5 = 10
3 * 5 = 15
4 * 5 = 20
5 * 5 = 25
6 * 5 = 30
7 * 5 = 35
8 * 5 = 40
9 * 5 = 45
10 * 5 = 50

Tuesday, 3 April 2018

Shell script to find n!/r!

# shell script to find n!/r!
echo "enter n and r value"
read n r
factn=1
for((i=1; i<=n; i++))
do
    factn=`expr $factn \* $i`
done
factr=1
for((i=1; i<=r; i++))
do
    factr=`expr $factr \* $i`
done
echo "n!/r! = `expr $factn / $factr`"




OUTPUT
Safi> bash nrfact.sh
enter n and r value
5 2
n!/r! = 60

shell script to count the login users and welcome them

#Script to count login users and welcome them
dt=`date +%H`
echo "No. of users currently logged in : `who|cut -d " " -f 1|uniq| wc -l`"
if [ $dt -lt 12 ] ; then
    echo "We welcome you all. Good Morning!"
elif [ $dt -lt 16 ] ; then
    echo "We welcome you all. Good Afternoon!"
else
    echo "We welcome you all. Good Evening!"
fi




OUTPUT
csc@csc-SVE1513CYNB ~ $ sh usrCnt.sh
No. of users currently logged in : 1
We welcome you all. Good Evening!

Monday, 2 April 2018

Shell script to prepare electricity bill

#Script to prepare Electricity Bill
echo "Enter the name"
read name
echo  "Enter the meter number"
read mno
echo "Enter the current month reading"
read cmr
echo "Enter the last month reading"
read lmr
unit=`expr $cmr - $lmr`
if [ $unit -eq 0 ]
then
       charge=40
elif [ $unit -gt 0 -a $unit -le 100 ]
then
      charge=`expr $unit \* 1`
elif [ $unit -gt 100 -a $unit -le 300 ]
then
     unit=`expr $unit - 100`   
     charge=`expr 100 + $unit`
elif [ $unit -gt 300 ]
then
    unit=`expr $unit - 300`   
    charge=`expr $unit \* 2`
    charge=`expr 300 + $charge`
fi
echo "EB Bill"
echo "---------"
echo "Consumer Name: $name"
echo "Meter Number : $mno"
echo "Units Consumed: `expr $cmr - $lmr`"
echo "Amount to be paid: $charge"


OUTPUT
Safi>sh EBbill.sh
Enter the name
ss
Enter the meter number
3340
Enter the current month reading
500
Enter the last month reading
100

EB Bill
---------
Consumer Name: ss
Meter Number : 3340
Units Consumed: 400
Amount to be paid: 500

Shell script to count the vowels in the given string

#script to count the number of vowels in the given string
echo "enter a string"
read str
count=0
len=${#str}
while [ $len -gt 0 ]
do
       ch=`echo "$str" | cut -c $len`
       case $ch in
               [aA] | [eE] | [iI] | [oO] | [uU] ) count=`expr $count + 1`;;
       esac
       len=`expr $len - 1`
done
echo "The number of vowels in the given string '$str' is $count"



OUTPUT
Safi>sh vowcnt.sh
enter a string
Aeroplane
The number of vowels in the given string 'Aeroplane' is 5

Shell script to find m to the power n



echo "Enter the m And n"
read m n
 
#Script to find m power n                                                                                        power=1                                                                                                                                      
for((i=1; i <= n; i++))
do                                                                                                                                                                                                                                                     
          power=`expr $power \* $m`
                                                                                                                       
done                                                                                                                                            
echo "$m power $n = $power"




OUTPUT

csc@csc-SVE1513CYNB ~ $ bash power.sh
Enter the m And n
4 3
4 power 3 = 64

Shell script to display user login time

#Script  to display the time at which specified user logged in
un=$1
t=`who | grep "$un" | head -1 | tr -s " " | cut -d ' ' -f 4`
echo "User $un has logged in at $t time"



OUTPUT
csc@csc-SVE1513CYNB ~ $ sh logTime.sh csc
User 'csc' has logged in at time 02:13



Shell script to perform basic operations on file


#Performs file operations
echo "Enter your choice"
echo "1. Copy 2. Remove 3. Rename 4. Exit"
read choice
if [ $choice -eq 1 ]
then
    echo "Enter the filename to be copied"
    read f1
    echo "enter the target filename"
    read f2
    cp $f1 $f2
fi
if [ $choice -eq 2 ]
then
    echo "enter the filename to be removed"
    read f1
    rm $f1
fi
if [ $choice -eq 3 ]
then
    echo "enter the file name to be renamed"
    read f1
    echo "Enter the file name to replace"
    read f2
    mv $f1 $f2
fi
if [ $choice -eq 4 ]
then
    exit 1
fi


OUTPUT
csc@csc-SVE1513CYNB ~ $ sh fileOp.sh
Enter your choice
1. Copy 2. Remove 3. Rename 4. Exit
1
Enter the filename to be copied
fileOp.sh
enter the target filename
ss
csc@csc-SVE1513CYNB ~ $ sh fileOp.sh
Enter your choice
1. Copy 2. Remove 3. Rename 4. Exit
2
enter the filename to be removed
ss
csc@csc-SVE1513CYNB ~ $ sh fileOp.sh
Enter your choice
1. Copy 2. Remove 3. Rename 4. Exit
3
enter the file name to be renamed
fileOp.sh
Enter the file name to replace
fOperations.sh
csc@csc-SVE1513CYNB ~ $ sh fileOp.sh
Enter your choice
1. Copy 2. Remove 3. Rename 4. Exit
4

Shell script to delete lines containing a pattern


#Deletes lines in a file which contains a given pattern
echo "Enter the pattern to be searched"
read p
n=$#
for i in $*
do
    if [ -f $i ]
        then   
                chk=`grep $p $i`
                if [ -z $chk ]
                then
                       echo "Pattern not found in file '$i'"
               else           
                       `grep -v $p $i > tfile`
                       echo "Before deletion"
                       cat $i
                       echo "After deletion"
                       cp tfile $i
                       cat $i
               fi
        else
               echo "File '$i' does not exist"
        fi
done




OUTPUT
csc@csc-SVE1513CYNB ~ $ sh ss.sh file2 ffff ff
Enter the pattern to be searched
Anu
Before deletion
103 Banu CIO
103 Banu CIO
103 Banu CIO
101 Anu CEO
104 Shan CFO
101 Sasha MD
After deletion
103 Banu CIO
103 Banu CIO
103 Banu CIO
104 Shan CFO
101 Sasha MD
File 'ffff' does not exist
Pattern not found in 'ff'