shell脚本例子
#!/bin/ksh
#ident"%W%"
functionsumRows{
cp$1$1.tmp
num=`greprows$1.tmp|awk'{print$2}'`
if[-z"$num"];then
num=0
fi
num=`echo"$num+$2"|bc`
grep-vrows$1.tmp>$1
echorows$num>>$1
rm$1.tmp
}
functionstartMarxJob{
if["$isTrackStatus"!="true"];then
return0;
fi
whilegetoptsj:p:c:m:d:args
do
case$argsin
j)JOB_name="$OPTARG";;
p)Pid="$OPTARG";;
c)COMMENT="$OPTARG";;
m)allowMultiExec="$OPTARG";;
d)date="$OPTARG";;
esac
done
SQL_CMD="sp_marx_job_start@jobname='$JOB_NAME',@pid=$PID,@jobdate=$date,@allowMultiExecution='${allowMultiExec}',@comment='$COMMENT',@id_return=@jobidout"
echoexec$SQL_CMD
jobTmpFile=${GCDR_TMPDIR}/.$$.sql.result.tmp
$ISQL$SQL<<EOD>$jobTmpFile
declare@jobidint
exec$SQL_CMD
go
EOD
if[$?!=0];then
echofailedtoexecsp_marx_job_start;
return-1;
fi
jobid=`grepJob_ID$jobTmpFile|awk'{print$2}'`
rm$jobTmpFile
if[-n"$jobid"];then
echoJob_ID$jobid;
echoJob_ID$jobid>${GCDR_TMPDIR}/.${PID}.status;
return0;
else
echofailedtostartjob;
return99;
fi
}
functionendMarxJob{
if["$isTrackStatus"!="true"];then
return0;
fi
whilegetoptsF:S:c:x:args
do
case$argsin
F)FILE="$OPTARG";;
S)STATUS="$OPTARG";;
c)COMMENT="$OPTARG";;
x)EXIT_STATUS="$OPTARG";;
esac
done
if[!-f$FILE];then
echo"$FILE"doesnotexists!;
return1;
fi
#grepjobidandrowprocessed
JOB_ID=`grepJob_ID$FILE|awk'{print$2}'`
ROWS_NUM=`greprows$FILE|awk'{print$2}'`
if[-z$ROWS_NUM];then
ROWS_NUM=0;
fi
if[finish="$STATUS"];then
SQL_CMD="sp_marx_job_finish$JOB_ID,$EXIT_STATUS,$ROWS_NUM,'$COMMENT'";
else
SQL_CMD="sp_marx_job_error$JOB_ID,$EXIT_STATUS,$ROWS_NUM,'$COMMENT'";
fi
echoexec$SQL_CMD
jobTmpFile=${GCDR_TMPDIR}/.$$.sql.result.tmp
$ISQL$SQL<<EOD>$jobTmpFile
exec$SQL_CMD
go
EOD
if[$?!=0];then
echofailedtoendjobduetoDBerror.;
return1;
fi
hasError=`grep-iERROR$jobTmpFile`
rm$jobTmpFile
if[-z"$hasError"];then
echoJob_ID$JOB_IDhasbeenmarkedas$STATUSsuccessfully.;
return0;
else
echoJob_ID$JOB_IDhasnotbeenmarkedas$STATUSsuccessfullydueto$hasError;
return1;
fi
}
functionstartMarxTask{
if["$isTrackStatus"!="true"];then
return0;
fi
whilegetoptsn:t:c:d:F:args
do
case$argsin
n)TASK_name="$OPTARG";;
t)TYPE="$OPTARG";;
c)COMMENT="$OPTARG";;
F)FILE="$OPTARG";;
esac
done
if[!-f$FILE];then
echojobstatusfiledoesnotexist.;
return99;
fi
#grepjobidandrowprocessed
JOB_ID=`grepJob_ID$FILE|awk'{print$2}'`
SQL_CMD="sp_marx_task_start$JOB_ID,'$TASK_NAME','$TYPE','$COMMENT',@taskidout"
echo$SQL_CMD
jobTmpFile=${GCDR_TMPDIR}/.$$.sql.result.tmp
$ISQL$SQL<<EOD>$jobTmpFile
declare@taskidint
exec$SQL_CMD
go
EOD
if[$?!=0];then
echofailedtoexecsp_marx_task_startduetoDBerror;
return1;
fi
taskid=`grepTask_ID$jobTmpFile|awk'{print$2}'`
rm$jobTmpFile
if[-n"$taskid"];then
grep-vTask_ID$FILE>$FILE.tmp;
echoTask_ID$taskid>>$FILE.tmp;
rm$FILE;
mv$FILE.tmp$FILE;
return0;
else
echofailedtogettaskID;
return99;
fi
}
functionendMarxTask{
if["$isTrackStatus"!="true"];then
return0;
fi
whilegetoptsx:s:f:c:r:S:F:args
do
case$argsin
x)EXIT_STATUS="$OPTARG";;
s)SUCCESS_NUM="$OPTARG";;
f)FAILURE_NUM="$OPTARG";;
c)COMMENT="$OPTARG";;
r)PROCESS_NUM="$OPTARG";;
S)STATUS="$OPTARG";;
F)FILE="$OPTARG";;
esac
done
if[!-f$FILE];then
echojobstatusfiledoesnotexist.;
return99;
fi
TASK_ID=`grepTask_ID$FILE|awk'{print$2}'`
if[finish="$STATUS"];then
SQL_CMD="sp_marx_task_finish$TASK_ID,$EXIT_STATUS,$SUCCESS_NUM,$FAILURE_NUM,'$COMMENT',@taskidout";
else
SQL_CMD="sp_marx_task_error$TASK_ID,$EXIT_STATUS,$SUCCESS_NUM,$FAILURE_NUM,'$COMMENT',@taskidout";
fi
echo$SQL_CMD
jobTmpFile=${GCDR_TMPDIR}/.$$.sql.result.tmp
$ISQL$SQL<<EOD>$jobTmpFile
declare@taskidvarchar(80)
exec$SQL_CMD
go
EOD
if[$?!=0];then
echofailedtoendtaskduetoDBerror;
return1;
fi
hasError=`grep-iERROR$jobTmpFile`
rm$jobTmpFile
if[-z"$hasError"];then
#addtherowsnum
sumRows$FILE$PROCESS_NUM;
echoTASK_ID$TASK_IDhasbeenmarkedas$STATUSsuccessfully.;
echo$PROCESS_NUMrowsbeenprocessed.;
return0;
else
echoTASK_ID$TASK_IDhasnotbeenmarkedas$STATUSsuccessfullydueto$hasError;
return1;
fi
}
functioninsertLogName{
if["$isTrackStatus"!="true"];then
return0;
fi
whilegetoptsl:F:args
do
case$argsin
l)LOGname="$OPTARG";;
F)FILE="$OPTARG";;
esac
done
if[!-f$FILE];then
echojobstatusfiledoesnotexist.;
return99;
fi
JOB_ID=`grepJob_ID$FILE|awk'{print$2}'`
SQL_CMD="insertintomarx_job_logvalues($JOB_ID,'${LOGNAME}')"
echo$SQL_CMD
jobTmpFile=${GCDR_TMPDIR}/sql.result.tmp
$ISQL$SQL<<EOD>$jobTmpFile
declare@taskidvarchar(80)
$SQL_CMD
go
EOD
if[$?!=0];then
echofailedtorecord$LOGNAMEtoJOB_ID$JOB_ID;
return1;
fi
hasError=`grep-iERROR$jobTmpFile`
rm$jobTmpFile
if[-z"$hasError"];then
echorecord$LOGNAMEtoJOB_ID$JOB_IDsuccessfully.;
return0;
else
echofailedtorecord$LOGNAMEtoJOB_ID$JOB_IDdueto$hasError;
return1;
fi
}
functioninsertMarxJob{
if["$isTrackStatus"!="true"];then
return0;
fi
PROCESS_NUM=0
COMMENT=""
whilegetoptsj:p:x:r:S:c:d:l:args
do
case$argsin
j)JOB_name="$OPTARG";;
p)Pid="$OPTARG";;
x)exit_status="$OPTARG";;
r)PROCESS_NUM="$OPTARG";;
S)status="$OPTARG";;
c)COMMENT="$OPTARG";;
d)date="$OPTARG";;
l)logname="$OPTARG";;
esac
done
SQL_CMD="insertintomarx_job_statusvalues('$JOB_NAME',$PID,$date,getdate(),getdate(),$exit_status,$status,$PROCESS_NUM,'$COMMENT')"
echo$SQL_CMD
jobTmpFile=${GCDR_TMPDIR}/.$$.sql.result.tmp
$ISQL$SQL<<EOD>$jobTmpFile
$SQL_CMD
print"Job_ID%1!starts",@@identity
go
EOD
if[$?!=0];then
echofailedtoinsertjob;
return-1;
fi
if[$logname=""];thenreturn0;fi
jobid=`grepJob_ID$jobTmpFile|awk'{print$2}'`
rm$jobTmpFile
if[-n"$jobid"];then
SQL_CMD_LOG="insertintomarx_job_logvalues($jobid,'$logname')"
echo$SQL_CMD_LOG
$ISQL$SQL<<EOD
$SQL_CMD_LOG
go
EOD
fi
}