Saturday, 2 November 2013

script to check whether File exists

# shell script to check whether file exist in a directory
# if exists then show last modification time
#!/bin/bash


echo "Enter file name "
read mfile
if [ ! -f $mfile ] ; then

echo "file does not exist"
exit 1
else
    echo "file exists"
    echo "Last modification time : `ls -l $mfile | cut -d " " -f 9` "
fi

String Palindrome

#shell script to check whether inputted string is palindrome

#!/bin/bash

echo "Enter string"
read myn
i=1
leng=`expr length $myn`
flag=true
while [ $i -le $leng ]
do
myf=`echo $myn | cut -c $i`
mylf=`echo $myn | cut -c $leng`

    if [ $myf != $mylf ]; then

flag=false
    fi
i=`expr $i + 1`
leng=`expr $leng - 1`
done

if [ $flag == true ]; then
echo "String is palindrome"
else
echo "String is not palindrome"

fi

Number's digits sum and length of number

# Check whether input value is number.
# if input value is number , check number is odd or even
# and find length of number and summation of digits

#!/bin/bash

echo "Enter number "
read no1

if [ -z $no1 ] ; then
echo "it is null"
exit 1
fi

#if [ `expr $no1 : "^[0-9]\+\.*[0-9]\+$"` == 0 ] ; then  # check decimal number

if [ `expr $no1 : "^[0-9]\+$"` == 0 ] ; then
   echo "no number"
   exit 1
fi

if [ `expr $no1 % 2` -eq 0 ] ; then
     echo "even number"
else
     echo "Odd number"

fi
# length of number

 echo "Length of number $no1  is :` expr length $no1`"
#sum of digits in a number
sum=0
while [ $no1 -gt 0 ]
 do
  mrem=`expr $no1 % 10`
  no1=`expr $no1 / 10`
  sum=`expr $sum + $mrem`

done
echo "Sum of digits: $sum "



Sort file base on line starting value

# file name is given as command line (First argument) "$1"
# reverse sort file content based on line start with value "$2" supplied as command line argument (Second Argument)

#!/bin/bash

if [ -z $1 ] || [ -z  $2  ]

then
    echo  "$0 : provide exact two argument "
    exit 1

else
    cat "$1" | grep "^$2"  | sort -r

fi  


Digit to Word convert

#A shell script to input number (max 6 digit) and print the number in word. E.g. 1234 output : one two  three four

#! /bin/bash

echo -e "Enter number"
read myno
if [ `expr $myno : "^[0-9]\+$"` == 0 ] ; then
   echo "no number"
   exit 1
fi


myrevno=`echo $myno | rev`
mylen=`expr length $myrevno`
 if [ $mylen -le 6 ]

then

 until [ $myrevno -le 0 ]
 do
mydno=`expr $myrevno % 10`
myrevno=`expr $myrevno / 10`
 

case $mydno in

1) echo -n "one " ;;
2) echo -n "two " ;;
3) echo -n "three " ;;
4) echo -n "four " ;;
5) echo -n "five " ;;
6) echo -n "six " ;;
7) echo -n "seven " ;;
8) echo -n "eight " ;;
9) echo -n "nine " ;;
0) echo -n "zero " ;;

esac
 done
else
echo -e "Please, enter max. 6 digiit number..."

fi
echo -e ""

awk and sed scripts example 2

#shell script to manage(add/update/view/delete) Judge database with Fields: JudgeName, CourtName,City,Cases_judged,TotalCasses
#1. Display No of records
#2. Find Judge with highest cases judged
#3. Calculate total Cases of Court “Ahmedabad”
#4. List All Judge Names
#5.Exit

#! /bin/bash

mych=0
until [ $mych -eq 5 ]
do

echo ""
echo " Select following choices "
echo " 1) Add records in judge file"
echo " 2) update records in judge file"
echo " 3) Display no of Records "
echo " 4) Delete records in judge file"
echo " 5) Exit "

read mych

case $mych in

   1) echo "Add Records to Judge File "
 echo -e "Enter Judge Name :"
 read jname
 read -p  "Enter Court Name : " cname
 read -p  "Enter City : " city
 read -p  "Enter Cases Judged :" cjudged
 read -p  "Enter Total Cases : " tcases
 echo -e $jname"\t"$cname"\t"$city"\t"$cjudged"\t"$tcases>>judgedb.dat ;;
   2) echo -e "Update Record for Judge "
read -p "Enter Judge Name whose info wants to update : " jname
echo -e "Enter cjudged"
read  cjudged  
   
oldcjudged=`awk -v jn=$jname ' $1==jn  {print $4 }' judgedb.dat`
    echo $oldcjudged  
lineno=`awk -v jn=$jname ' $1==jn  {print FNR }' judgedb.dat`
        echo $lineno
sed "$lineno s/\<$oldcjudged\>/$cjudged/1" judgedb.dat ;;
   3) awk -f noofrecordsjudge.awk judgedb.dat ;;
   4) echo -e "Delete Record for Judge "
read -p "Enter Judge Name whose info wants to be deleted : " djname
rcount=`grep -c "^$djname" judgedb.dat`
if [ $rcount -eq 1 ];
then
 sed "/^$djname/d" judgedb.dat
 # to delete permanently from file use
 # sed -i "/^$djname/d" judgedb.dat

 echo -e "Record deleted successfully"
else
 echo -e "Record does not exist"
fi
;;
   5) exit ;;

esac

done


# Display No of records
# Command is
awk " END { print NR -1 } " judgedb.dat


#Find Judge with highest cases judged
# Command is
awk -f  judgehighcase.awk judgedb.dat

# awk language code is infile named as judgehighcase.awk

# Preprocessing section "BEGIN " is optional here
BEGIN { 
          }
{
    if (FNR==2) { highcase=$4 }
    if (highcase<$4)
           { highcase=$4
              judge=$1 
            }
}
 END { 
          print judge " with highest cases : " highcase
 }

#Calculate total Cases of Court “Ahmedabad”
# Command is
awk -f  totalcases.awk judgedb.dat

# awk language code is infile named as totalcases.awk
{
  if ($3=="AHMEDABAD")
{
    tcases+=$5
}

}

END {
print "total cases of Ahmedabad court :" tcases
}


# List All Judge Names
# Command is 
awk -f listalljudgenames.awk judgedb.dat

# awk language code is infile named as listalljudgenames.awk

BEGIN { print "All judge names : " 
}
{
if ($1=="JudgeName") { FNR=2}
  else
   print $1
}


# Master menu driven shell script file to display record, find records

#! /bin/bash

mych=0
until [ $mych -eq 5 ]
do

echo ""
echo " Select following choices "
echo " 1) Display no of Records "
echo " 2) Find judge with highest cases "
echo " 3) Calculate total cases of court "
echo " 4) List all judge names "
echo " 5) Exit "

read mych

case $mych in

   1) awk " END { print NR -1 } " judgedb.dat ;;

   2) awk -f judgehighcase.awk judgedb.dat ;;

   3) awk -f totalcases.awk judgedb.dat ;;

   4) awk -f listalljudgenames.awk judgedb.dat ;;

   5) exit ;;

esac

done

# Data File named judgedb.dat

JudgeName       CourtName       City            Cases_judged    TotalCasses
AAA             CIVIL           AHMEDABAD       40              100
BBB             NONCIVIL        AHMEDABAD       10              40
CCC             CRIMINAL        BARODA          40              80
DDD             CIVIL           BARODA          40              140
EEE             CIVIL           ANAND           15              100
FFF             NONCIVIL        SURAT           40              40
TTT     NONCIVIL        SURAT   40      60
XXX     VOCATIONAL      ANAND   4      40
WWW     CIVIL   SURAT   50      60
ZZZ CIVIL   BARODA  40      60
ZZX     CIVIL   ANAND   40      40
YYY NONCIVIL   BARODA  20      30
PPP CIVIL   MUMBAI  60      100

Simple Encryption Example

#  Enter four-digit number and Perform Encryption
# e.g. 5678 will be displayed as 4321 Rule : 9-5 = 4, 9-6=3, 9-7=2,9-8=1 i.e. 4321

#!/bin/bash
#set -x

echo "Enter your no"
read myno
revno=0
while [ $myno -gt 0 ] ;
do
digit=`expr $myno % 10`
myno=`expr $myno / 10`
encodeno=`expr 9 - $digit`
revno=`expr $encodeno + $revno \* 10`
done

echo $revno | rev