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

38 comments:

帶我走 said...

肯定與支持你!!!加油囉~.........................

好想好好休息 said...

大家隨便說,說什麽都好,就是請不要惦惦 ....................................................

開心唷 said...

I love readding, and thanks for your artical. ........................................

怡伶 said...

廢話不多,祝你順心~^^........................................

V定治 said...

Never put both feet in your mouth at the same time, because then you will not have a leg to stand on.............................................

林淑凡 said...

幸福不是一切,人還有責任。......................................................

Porsha蔡Coghlan明宏 said...

能猜得出女人真實年齡的男人也許耳聰目明,但肯定毫無大腦。哈哈!......................................................

o0625TaylorJ_Duraz said...

很棒的分享~~~來留個言囉~~~~ .........................................

ZenaT_Pinter2284 said...

the best as always thanks ........................................

Glennie9654 said...

adult成人情色激情成人聊天網情色影片色情俱樂部性愛影片情趣淫水影音情色限制級照片live show女生自慰影片超屌成人情色留言板淫亂女自慰免費視訊辣妹巨乳鹹濕色情影音聊天自拍走光照片免費情色av圖片亞洲情色論壇淫慾免費裸女圖片裙底風光台灣色情論壇一夜正妹淫娃網情色下載a圖片全裸淫蕩女人情色影音聊天高潮自慰色情圖片成人自拍女生自慰亞洲成人色情dvd舔乳頭女生自慰影片台灣色情網站台灣性樂園sex story情色影音激情聊天室嘿咻情色自拍

佩璇 said...

Thank you for Posts~............................................................

惠玲 said...

死亡是悲哀的,但活得不快樂更悲哀。.........................

柏勳 said...

幸福不是一切,人還有責任。..................................................

SeymourZ_Reger嘉容 said...

your son/daughter is so cute............................................................

Burma家銘ege said...

黃金千金,不如季布一諾。 ............................................................

adkinsra said...

生存乃是不斷地在內心與靈魂交戰;寫作是坐著審判自己。 ..................................................

溫淑芬 said...

大師手筆﹐果然不凡.................................................................

婷珊 said...

人不能像動物一樣活著,而應該追求知識和美德....................................................................

亦妮 said...

河水永遠是相同的,可是每一剎那又都是新的。......................................................................

萱祥 said...

所有的資產,在不被諒解時,都成了負債............................................................

玉苓玉苓 said...

向著星球長驅直進的人,反比踟躕在峽路上的人,更容易達到目的。............................................................

王名仁 said...

人有兩眼一舌,是為了觀察倍於說話的緣故。............................................................

雅莊王edgd春2蕙婷余惠其 said...

No one knows the weight of anothers burden. ............................................................

竹青 said...

偶爾上來逛逛,下次不知是否還有緣再進來,先祝您平安順利!!!............................................................

育隆 said...

開心不開心都是一天,祝您能夠笑著面對一切!............................................................

吳婷婷 said...

河水永遠是相同的,可是每一剎那又都是新的。..................................................

溫緯李娟王季 said...

今天是個好天氣~祝你愉快~^^~~..................................................................

誠紋 said...

快下班囉~來幫你加油~~.................................................................

琬安琬安 said...

最敏捷的,未必贏得競賽:最強大的,未必贏得戰爭:時間與機會才是主人................................................

RicoLisi0802志竹 said...

Joy often comes after sorrow, like morning after night.............................................................

楊燕沛楊燕沛 said...

you got it!i like it!!!..................................................................

駱李淑華明欣 said...

人生中最好的禮物就是屬於自己的一部份............................................................

黃沈貞儀吉軍 said...

不會從失敗中找尋教訓的人,成功之路是遙遠的。.......................................................

少明秋菁 said...

走召糸及言贊白勺口拉............................................................

文王廷 said...

Subtlety is better than force. ......................................................................

偉曹琬 said...

唯有用熱情、用智慧去觀察事物,這事物才會把他的秘密,洩漏給我們......................................................................

智柏林婉林亞 said...

友誼能增進快樂,減少痛苦......................................................................

翊翊翊翊張瑜翊翊翊 said...

人生中最好的禮物就是屬於自己的一部份..................................................

About Me

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