[TriLUG] help converting a perl script to zshell

Smith, Brett bsmith at bloodhoundinc.com
Wed Apr 14 15:27:33 EDT 2004


Fixed the case at the end of the script... It is still ugly.

#!/bin/zsh
##				filesanity.zsh
## 			REVISION 5 Tue 13 Apr 2004 09:24:41 AM EDT 
##	     File Sanity Check -0.9.5 File Sanity Check Script for FTP
Server
##	    File Sanity Check for incoming files before they enter the
Workflow
##			Tue 13 Apr 2004 09:24:51 AM EDT 		
##			   	Brett Smith				
##
## You Break it you get to keep all the pieceZ
## Setup Variables
#set -x # for debugging
			    	

##PATH SPECIFICS
DIR="/secure"
CDDEV="/dev/cdrom"
FDDEV="/dev/fd0"
HST=`hostname`
DATE=`date "+%m%d%y %H:%M"` ##MonthDayYear Hr:Min
FDATE=`date`
DIST=`grep Linux /etc/issue`
KVER=`uname -r`

##FILE SANITY SPECIFICS
BASENAME="filesanity.zsh"
BDDATE=`date "+%m%d%y%H%M"` ##MonthDayYearHrMin
BDDIR="$2"
BDLOG="${BDDIR}-${BDDATE}-BDF.log"
BDTITLE="File Sanity Check -0.9.5 File Sanity Check Script\n"
BDBTITLE="File Sanity Check for incoming files before they enter the
Workflow\n"
BDEXTRA="This is the file sanity script\nIt checks for metacharacters in
filenames\n"
BDSUBJECT="Files moved from $BDDIR on $DATE"
BDERROR="Warning $BDTITLE Malfunction $DATE"
BDUSER="helpdesk at bhsoftware.com"
BDTAR="${BDDATE}-BDF.tar.bz2"
BDTDIR="${HOME}/tmp"
BDLIST="${BDDIR}-badfilelist"

#Logging and Email

usageBD(){
	echo -en "${BDTITLE}${BDBTITLE}${BDEXTRA}\n[user at hostname]:$
./$BASENAME /path/to/check\n"
}

errorBD(){
	echo -en "The $BDTITLE\nExited with Error Status $? \n"| mail -s
"$BDERROR" $BDUSER
}

mailBD(){
	echo -en "$FDATE Found $NUMBD badcharacters in $DIR
filenames.\nMoved to $BDLIST to $BDTAR and\nremoved files from
workflow.\n$BDTAR can be found in $BDDIR\nLIST:\n${chk4BD}\n"| mail -s
"$BDSUBJECT" $BDUSER
}

setBDLOG(){
if [ -f $BDLOG ];
	then 	
		rm -f $BDLOG
		touch $BDLOG
		logBD "$BDTITLE\n"
		logBD "$FDATE\n";
	else	
		errorBD;
fi
}

logBD(){
	echo -n "$@" >> $BDLOG
}
BDout(){
	echo -en "$@" | tee -a $BDLOG
}

chk4BD(){
if [ -d $BDDIR ];
	then
		ls $BDDIR|grep  '[]*?~=/&;!#$%^(){}<>[]';
fi
}

fndBDF(){
NUMBD=`chk4BD|wc -w|awk '{print $1}'`
if [ -f $BDLIST ];
	then 
		rm -f $BDLIST;
fi
if [ "$NUMBD" = 0 ];
	then 
		BDout "All is well...\n"   
		BAD=0;
	else
		
		chk4BD >> $BDLIST 
		BDout "Found $NUMBD bad characters in Files:\n${chk4BD}\n"
		BAD=1;
fi

}



workBDF(){
	if [ "$BAD" = "1" ];
		then
			tar -jcvf $BDTDIR/$BDTAR --remove-files $BDDIR
			#mailBD	
	fi 
			
}



##DEBUG CODE	
testBD(){
if [ "$BAD" = "1" ];
	then
		BDout "Found some bad filenames\n"
		BDout "This is the number of Bad Characters\n"
		BDout "NUMBD=$NUMBD\n"
		BDout "This is the on/off switch\n"
		BDout "BAD=$BAD\n"
		BDout "This is the temp dir\n"
		BDout "TEMP=$BDTDIR\n"
		BDout "This is the Directory checked\n"
		BDout "DIR=$BDDIR\n"
		BDout "This is the tar command to remove the files\n"
		BDout "tar -jcvf $BDTDIR/$BDTAR --remove-files $BDIR\n"
		BDout "These are the files\n"
		BDout "`cat $BDLIST`\n"
	else
		BDout "All is well in $BDDIR\n"
		BDout "This is the number of Bad Characters\n"
		BDout "NUMBD=$NUMBD\n"
		BDout "This is the on/off switch\n"
		BDout "BAD=$BAD\n"
		BDout "This is the temp dir\n"
		BDout "TEMP=$BDTDIR\n"
		BDout "This is the Directory checked\n"
		BDout "DIR=$BDDIR\n"
fi

}
##Do some real work
NOARGS=0 
EXITERROR=65
 
if [ $# -eq "$NOARGS" ] 
	then	
		usageBD
		exit $EXITERROR   
fi  




##Check for help switch
while getopts ":hvt:r:" OPT;
do
  case $OPT in
	h | --help    	) 	usageBD 
			  	;;
	v | --version 	) 	usageBD 
			  	;;
	t | --test	)	setBDLOG
				chk4BD
				fndBDF
				testBD
				;;
	r | --run	)	setBDLOG
				chk4BD
				fndBDF
				workBDF
				;;
	*		)	usageBD
				;;
		  
  esac
done
shift $(($OPTIND - 1))
exit 0	

This email message is for the sole use of the intended recipients(s) and may contain confidential and privileged information of Bloodhound Software, Inc.. Any unauthorized review, use, disclosure is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.



More information about the TriLUG mailing list