Wednesday, April 9, 2008

Sample Shell scripts Examples:

Shell scripts use to automate your daily tasks and make life peaceful
===================================================================================== A simple 3 line shell script to check if the doent exists then backup the logfile with date suffix and create the new logfile. if the file exists then do nothing
#!/usr/bin/ksh
if [[ ! -f /home/jack/app/donotrun.tmp ]] ; then
mv /tmp/app.log /tmp/app.log.`date +%Y%m%d%H%M`

touch /tmp/app.log
fi
____________________________________________________________________
Examples 1 .

  • Sample code to automate FTP in shell script.
    ############ Create the file needed for FTP ################
    count=`find ${PDFPUB} -type f wc -l` #To checking how many files are there to transfer
    echo open $FTP_HOST > FINPUB_FTP.temp # host name stored in variable
    echo "user ${FINPUBUSER} ${FINPUBPASS}" >> FINPUB_FTP.temp
    echo binary >> FINPUB_FTP.temp
    echo "cd /tmp/FINPUB" >> FINPUB_FTP.temp
    echo "lcd /home/mpansare/pdfpub" >> FINPUB_FTP.temp
    echo prompt >> FINPUB_FTP.temp
    echo "mput *" >> FINPUB_FTP.temp
    echo bye >> FINPUB_FTP.temp
    ############ FTP the file ################
    echo "" >>$LOGFILE
    ftp -nv -i <>>$LOGFILE
    rm FINPUB_FTP.temp
    ------------------------------------------------------------------------------------------------- ftp options:
    -v = Verbose option forces ftp to show all responses from the remote server, as well as report on data transfer statistics.
    -n = Restrains ftp from attempting ``auto-login'' upon initial connection.
    -i = Turns off interactive prompting during multiple file transfers.
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ########## Determine if FTP was successful ################
    grep "226 Transfer complete." $LOGFILE
    return_code=$?
    grepcount=`grep -c "226 Transfer complete" $LOGFILE` # counts how many files were transfered properly

    # If else block
    if [[ $count -eq $grepcount ]]; then
    echo "FTP file transfer counts match" >> $LOGFILE
    else
    echo "FTP files transfer donot match. Some files may not have transfer" >> $LOGFILE
    fi

=====================================================================================

Example 2.

  • Script to check diskusage that runs thru cron every hour to report and alert if disk space exceeds 95%

#!/usr/bin/ksh
#############################################################
# Script Name:checkDiskUsage.sh

###########################################################
dater=`date +%Y%m%d`
sScriptName=checkDiskUsage
sLogfile=/dev/${sScriptName}.${dater}.log
sSpool=/tmp/${sScriptName.$$}.tmp
echo "${sScriptName} starting on `date`" >> ${sLogfile}
iSpoolCmd=`df -k egrep -v "/procFilesystem" > "${sSpool}"`
if [[ $iSpoolCmd -ne 0 ! -s ${sSpool} ]];then
echo "Error: Could not retrieve disk usage statistics. Aborting!" >> $sLogfile
exit 255
fi
while read -r myline; do
sDir=`echo "${myline}" awk '{print $7}'`
sPUsage=`echo "${myline}" awk '{print $4}'`
sUsage=`echo "${sPUsage}" tr -d %`
#echo ${sDir}, ${sUsage}

if [[ ${sUsage} -ge 95 ]];then
echo "WARNING : Usage on ${sDir} has reached ${sPUsage}" >> ${sLogfile}
elif [[ ${sUsage} -ge 90 && ${sUsage} -lt 95 ]];then
echo "NEEDS ATTENTION : Usage on ${sDir} has reached ${sPUsage}" >> ${sLogfile}
fi
done < ${sSpool} echo "${sScriptName} ending on `date`" >> ${sLogfile}
rm ${sSpool} >/dev/null
exit 0

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

=====================================================================================

Example 3.

  • The dailylogbackup script is used to backup the log files on dailybasis with date suffix This script is called with a PARM file which lists all the logfiles to be backed up (on a daily basis).# Input parms:

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

#!/usr/bin/ksh
#############################################################
# ScriptName: dailyBkupLogFile.sh
# Purpose: c
# Input parms:
# Usage: dailyBkupLogFile.sh
# Parent script:
#################################################################
# Define a path where the script will log all actions
mylogfile=/dev/Logs/dailyBkupLogFile.log
# Check for proper command line arguments
if [ $# -ne 1 ] ; then
echo "Incorrect Usage. Correct Usage: $0 "
exit 1
fi
# Check if the PARM file really exists
parmfile=$1
if [ ! -f $parmfile ] ; then
echo "Error: C
ould not find the specified PARM file. Please verify the PARM file exists."
exit 1
fi
echo "$0 started on `date`" >> $mylogfile
# Iterate through the list of logfiles supplied in the PARM file
while read -r sLine ; do
if [ -f $sLine ] ; then
#create dated name
newfile="$sLine.`date +%Y%m%d`"
#flag and counter to tack on the end
i=0
#if the newfile exists, set flag
if [ -f $newfile ] ; then
i=1
#loop to see how many we need to do
while [ -f $newfile.$i ]; do
((i+=1)) ; done

fi

#if there were previous logs, append the number
if [ $i -ne 0 ] ; then
mvcmd="mv $sLine $newfile.$i"
else
mvcmd="mv $sLine $newfile"
fi
# Backup the logfile by performing move/rename with current date appended
$mvcmd
if [ $? -eq 0 ] ; then
echo "Backing up logfile $sLine : SUCCESS" >> $mylogfile
else
echo "Backup command failed: $mvcmd" >> $mylogfile
fi
# Create a new logfile with the same original filename
touchcmd="touch $sLine"
$touchcmd
if [ $? -eq 0 ] ; then
echo "Creating logfile $sLine : SUCCESS" >> $mylogfile
else
echo "Logfile create failed: $touchcmd" >> $mylogfile
fi
else
echo "Error: Could not find logfile - Skipping backup of $sLine." >> $mylogfile
fi
done < $parmfile echo "$0 completed on `date`" >> $mylogfile
exit 0

No comments:

About Me

This site will be helpful for Unix system administrator to automate their tasks using scripts, comments on posts are welcome.