Tuesday 3 January 2012

Shell Script Programs.

2.Back up of all database

#!/bin/bash
export DB_BACKUP="/usr/local/bin/backup"
export DB_USER="root"
export DB_PASSWD="123"


echo ""
echo "Backup and rotate all mysql databases"
echo "--------------------------"

rm -rf $DB_BACKUP/04
mv $DB_BACKUP/03 $DB_BACKUP/04
mv $DB_BACKUP/02 $DB_BACKUP/03
mv $DB_BACKUP/01 $DB_BACKUP/02
mkdir $DB_BACKUP/01


echo "* Creating backup..."
mysqldump --user=$DB_USER --password=$DB_PASSWD --all-databases | bzip2 > $DB_BACKUP/01/mysql-`date +%Y-%m-%d`.bz2
echo "----------------------"
echo "Done"





3.CHANGE CASE FROM UPPER TO LOWER AND LOWER TO UPPER

#!/bin/bash
echo "enter"
echo "1 for uppercase"
echo "2 for lowercase"
read n
echo "enter file name"
read file
#for file in `ls`
if [ -f $file ]
then
if [ $n = 1 ]
then
up=`echo $file | tr '[ a-z ]' '[ A-Z ]'`
#if [ $up != $file ]; then
mv -i $file $up
fi

if [ $n = 2 ]
then
lc=`echo $file | tr '[ A-Z ]' '[ a-z ]'`
#if [ $up != $file ]; then
mv -i $file $lc
fi

#mv $file `echo $file | tr '[ a-z ]' '[ A-Z ]'`
fi



3.APPLICATION OF GETOPTS



#!/bin/bash
#myops()
#{
#  echo "Usage: $0 -h -c -e"
#  echo "Options: These are optional argument"
#  echo " -h help on how to use this script"
#  echo " -c wishes based on time"
#  echo " -e search and delete the file "filename" from the located path "
#  echo " their values are not taken)"
#  exit 1
#}

helps()
{
echo "This command is used to check valid command line argument are passed to script. Usually used in while loop.
Syntax:
getopts {optsring} {variable1}

getopts is used by shell to parse command line argument.

examlpe:
We have script called ani which has syntax as
ani -n -a -s -w -d
Options: These are optional argument
    -n name of animal
    -a age of animal
    -s sex of animal
    -w weight of animal
    -d demo values (if any of the above options are used their values are not taken)"
}

wish()
{
temp=`date +%H`
#temp=`date | cut -c12-13`
echo $temp
if [ $temp -lt 12 ]
then
echo "Good morning"
#fi
elif [ $temp -ge 12 -a $temp -lt 16 ]
then
echo "Good afternoon"
#fi
elif [ $temp -ge 16 -a $temp -lt 20 ]
then
echo "Good evng"
#fi
#if [ $temp -gt 20 ]
#then
else
echo "Good ni8"
fi
}

del()