日期:2014-05-16  浏览次数:20559 次

HP UNIX shell_异常处理
以ftp功能为例
#*****************************
#description 	ftp to get file
#input       	tIp tUser tPasswd tCd tLcd tFile
#output 	ftpStatus
#*****************************
ftpGetFile(){
tIp=$1
tUser=$2
tPasswd=$3
tCd=$4
tLcd=$5
tFile=$6
now=`date +%Y%m%d%H%M%S`
error=$now$RANDOM".temp"
/usr/bin/ftp -i -n $tIp<<PUTFILES>>$error
user $tUser $tPasswd
bin
hash
cd $tCd
lcd $tLcd
mget $tFile
bye
PUTFILES

loginFailed=`cat $error | grep "Login failed"`
notConnected=`cat $error | grep "Not connected"`

loginFailedLen=`echo $loginFailed |  awk '{ print length($0) }'`

notConnectedLen=`echo $notConnected |  awk '{ print length($0) }'`

ftpStatus=0
if [ $loginFailedLen != 0 ];
	then
	ftpStatus=1
	echo "login failed"
	elif [ $notConnectedLen != 0 ];
	then
	ftpStatus=1
	echo "not Connected"
	else
	ftpStatus=0
	echo "success"
fi
rm *.temp
return $ftpStatus
}