zgcsy1986 发表于 2013-2-1 11:57:20

shell脚本例子

#!/bin/ksh
#ident"%W%"
function sumRows {
cp $1 $1.tmp
num=`grep rows $1.tmp | awk '{print $2}'`
if [ -z "$num" ] ; then
    num=0
fi

num=`echo "$num+$2" | bc`

grep -v rows $1.tmp > $1
echo rows $num >> $1
rm $1.tmp
}

function startMarxJob {
if [ "$isTrackStatus" != "true" ] ; then
return 0;
fi
while getopts j:p:c:m:d: args
do
          case $args in
          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=@jobid out"
echo exec $SQL_CMD

jobTmpFile=${GCDR_TMPDIR}/.$$.sql.result.tmp
$ISQL $SQL <<EOD > $jobTmpFile
declare @jobid int
exec $SQL_CMD
go
EOD

if [ $? != 0 ] ; then
    echo failed to exec sp_marx_job_start;
    return -1;
fi

jobid=`grep Job_ID $jobTmpFile | awk '{print $2}'`

rm $jobTmpFile
if [ -n "$jobid" ] ; then
    echo Job_ID $jobid;
    echo Job_ID $jobid > ${GCDR_TMPDIR}/.${PID}.status;
    return 0;
else
    echo failed to start job;
    return 99;
fi
}

function endMarxJob {
if [ "$isTrackStatus" != "true" ] ; then
return 0;
fi
while getopts F:S:c:x: args
do
          case $args in
          F)FILE="$OPTARG";;
          S)STATUS="$OPTARG";;
          c)COMMENT="$OPTARG";;
          x)EXIT_STATUS="$OPTARG";;
          esac
done

if [ ! -f $FILE ] ; then
    echo "$FILE" does not exists!;
    return 1;
fi

#grep job id and row processed
JOB_ID=`grep Job_ID $FILE | awk '{print $2}'`
ROWS_NUM=`grep rows $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

echo exec $SQL_CMD

jobTmpFile=${GCDR_TMPDIR}/.$$.sql.result.tmp
$ISQL $SQL <<EOD > $jobTmpFile
exec $SQL_CMD
go
EOD


if [ $? != 0 ] ; then
    echo failed to end job due to DB error.;
    return 1;
fi

hasError=`grep -i ERROR $jobTmpFile`

rm $jobTmpFile
if [ -z "$hasError" ] ; then
    echo Job_ID $JOB_ID has been marked as $STATUS successfully.;
    return 0;
else
    echo Job_ID $JOB_ID has not been marked as $STATUS successfully due to $hasError;
    return 1;
fi
}

function startMarxTask {
if [ "$isTrackStatus" != "true" ] ; then
return 0;
fi
while getopts n:t:c:d:F: args
do
          case $args in
          n)TASK_NAME="$OPTARG";;
          t)TYPE="$OPTARG";;
          c)COMMENT="$OPTARG";;
          F)FILE="$OPTARG";;
          esac
done

if [ ! -f $FILE ] ; then
    echo job status file does not exist. ;
    return 99;
fi

#grep job id and row processed
JOB_ID=`grep Job_ID $FILE | awk '{print $2}'`
SQL_CMD="sp_marx_task_start $JOB_ID, '$TASK_NAME', '$TYPE', '$COMMENT' , @taskid out"
echo $SQL_CMD
jobTmpFile=${GCDR_TMPDIR}/.$$.sql.result.tmp
$ISQL $SQL <<EOD > $jobTmpFile
declare @taskid int
exec $SQL_CMD
go
EOD

if [ $? != 0 ] ; then
    echo failed to exec sp_marx_task_start due to DB error;
    return 1;
fi

taskid=`grep Task_ID $jobTmpFile | awk '{print $2}'`

rm $jobTmpFile
if [ -n "$taskid" ] ; then
    grep -v Task_ID $FILE > $FILE.tmp;
    echo Task_ID $taskid >> $FILE.tmp;
    rm $FILE;
    mv $FILE.tmp $FILE;
    return 0;
else
    echo failed to get task ID;
    return 99;
fi
}


function endMarxTask {
if [ "$isTrackStatus" != "true" ] ; then
return 0;
fi
while getopts x:s:f:c:r:S:F: args
do
          case $args in
          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
    echo job status file does not exist. ;
    return 99;
fi

TASK_ID=`grep Task_ID $FILE | awk '{print $2}'`

if [ finish = "$STATUS" ] ; then
    SQL_CMD="sp_marx_task_finish $TASK_ID , $EXIT_STATUS , $SUCCESS_NUM , $FAILURE_NUM , '$COMMENT' , @taskid out";
else
    SQL_CMD="sp_marx_task_error $TASK_ID , $EXIT_STATUS , $SUCCESS_NUM , $FAILURE_NUM , '$COMMENT' , @taskid out";
fi
echo $SQL_CMD

jobTmpFile=${GCDR_TMPDIR}/.$$.sql.result.tmp

$ISQL $SQL <<EOD > $jobTmpFile
declare @taskid varchar(80)
exec $SQL_CMD
go
EOD


if [ $? != 0 ] ; then
    echo failed to end task due to DB error;
    return 1;
fi

hasError=`grep -i ERROR $jobTmpFile`

rm $jobTmpFile
if [ -z "$hasError" ] ; then
    #add the rows num
    sumRows $FILE $PROCESS_NUM;
    echo TASK_ID $TASK_ID has been marked as $STATUS successfully.;
    echo $PROCESS_NUM rows been processed.;
    return 0;
else
    echo TASK_ID $TASK_ID has not been marked as $STATUS successfully due to $hasError;
    return 1;
fi
}

function insertLogName {
if [ "$isTrackStatus" != "true" ] ; then
return 0;
fi
while getopts l:F: args
do
          case $args in
          l)LOGNAME="$OPTARG";;
          F)FILE="$OPTARG";;
          esac
done

if [ ! -f $FILE ] ; then
    echo job status file does not exist. ;
    return 99;
fi

JOB_ID=`grep Job_ID $FILE | awk '{print $2}'`

SQL_CMD="insert into marx_job_log values ( $JOB_ID , '${LOGNAME}' )"

echo $SQL_CMD

jobTmpFile=${GCDR_TMPDIR}/sql.result.tmp

$ISQL $SQL <<EOD > $jobTmpFile
declare @taskid varchar(80)
$SQL_CMD
go
EOD


if [ $? != 0 ] ; then
    echo failed to record $LOGNAME to JOB_ID $JOB_ID;
    return 1;
fi

hasError=`grep -i ERROR $jobTmpFile`

rm $jobTmpFile
if [ -z "$hasError" ] ; then
    echo record $LOGNAME to JOB_ID $JOB_ID successfully.;
    return 0;
else
    echo failed to record $LOGNAME to JOB_ID $JOB_ID due to $hasError;
    return 1;
fi
}




function insertMarxJob {
if [ "$isTrackStatus" != "true" ] ; then
return 0;
fi
PROCESS_NUM=0
COMMENT=""
while getopts j:p:x:r:S:c:d:l: args
do
          case $args in
          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="insert into marx_job_status values ('$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
    echo failed to insert job;
    return -1;
fi

if [ $logname = "" ] ; then return 0 ; fi
jobid=`grep Job_ID $jobTmpFile | awk '{print $2}'`

rm $jobTmpFile
if [ -n "$jobid" ] ; then
    SQL_CMD_LOG="insert into marx_job_log values ($jobid, '$logname')"
    echo $SQL_CMD_LOG
    $ISQL $SQL <<EOD
$SQL_CMD_LOG
go
EOD
fi
}
页: [1]
查看完整版本: shell脚本例子