×

Loading...
Ad by
  • 技多不压身,工到自然成:安省技工证书特训班,点击咨询报名!
Ad by
  • 技多不压身,工到自然成:安省技工证书特训班,点击咨询报名!

请教unix shell 高手,看看下面的程序,为什么 mv 文件的部分被执行了两次。

本文发表在 rolia.net 枫下论坛#!/bin/ksh

# NAME: cleanup()
#
# DESCRIPTION: unsets traps, issues error messages and exits with a return code
#
# INPUT:
# $1 = exit code
# $2 = message if not null
#
cleanup ()
{
ec=$1
msg=$2

if [ $ec != 0 ]
then
printf "Batch Job Failure.....\n" >>$LOG_FILE
else
printf "Job completed; return code $ec DATE:`now`\n" >>$LOG_FILE
fi
trap -
exit "$ec"
}


################### main ##################

{
#####
/usr/sbin/mknod /tmp/exp_pipe p
compress </tmp/exp_pipe>/u06/export/fullexp_wacarc.dmp.Z &

ORA_EXP_FILE="/tmp/exp_pipe"
########
export LOG_DIR ORA_EXP_FILE

LOG_FILE=$LOG_DIR/fullexp_wacarc.log
JOB_SERVER=`hostname`
JOB_NAME=`basename $0 .sh`

DATETIME=`date '+%Y%m%d'`

mv /u06/export/day_2/fullexp_wacarc.dmp.Z /u06/export/day_3
mv /u06/export/day_1/fullexp_wacarc.dmp.Z /u06/export/day_2
mv /u06/export/fullexp_wacarc.dmp.Z /u06/export/day_1

#
# Trap on exit/interrupt/break to clean up
#

trap "cleanup 99 \">>> Abnormal program termination..."\" 1 2 15

#
# Initialize the logfile...
#
printf "Server: $JOB_SERVER\n" >$LOG_FILE
printf "Job: $JOB_NAME started DATE:`now`\n" >>$LOG_FILE

$ORACLE_HOME/bin/exp userid=system/${sidpwd} file=$ORA_EXP_FILE \
log=$LOG_DIR/full_backup.log full=y

RC=$?
[ $RC != 0 ] &&cleanup $RC "Export wacarc"
cleanup 0
}更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / 请教unix shell 高手,看看下面的程序,为什么 mv 文件的部分被执行了两次。
    本文发表在 rolia.net 枫下论坛#!/bin/ksh

    # NAME: cleanup()
    #
    # DESCRIPTION: unsets traps, issues error messages and exits with a return code
    #
    # INPUT:
    # $1 = exit code
    # $2 = message if not null
    #
    cleanup ()
    {
    ec=$1
    msg=$2

    if [ $ec != 0 ]
    then
    printf "Batch Job Failure.....\n" >>$LOG_FILE
    else
    printf "Job completed; return code $ec DATE:`now`\n" >>$LOG_FILE
    fi
    trap -
    exit "$ec"
    }


    ################### main ##################

    {
    #####
    /usr/sbin/mknod /tmp/exp_pipe p
    compress </tmp/exp_pipe>/u06/export/fullexp_wacarc.dmp.Z &

    ORA_EXP_FILE="/tmp/exp_pipe"
    ########
    export LOG_DIR ORA_EXP_FILE

    LOG_FILE=$LOG_DIR/fullexp_wacarc.log
    JOB_SERVER=`hostname`
    JOB_NAME=`basename $0 .sh`

    DATETIME=`date '+%Y%m%d'`

    mv /u06/export/day_2/fullexp_wacarc.dmp.Z /u06/export/day_3
    mv /u06/export/day_1/fullexp_wacarc.dmp.Z /u06/export/day_2
    mv /u06/export/fullexp_wacarc.dmp.Z /u06/export/day_1

    #
    # Trap on exit/interrupt/break to clean up
    #

    trap "cleanup 99 \">>> Abnormal program termination..."\" 1 2 15

    #
    # Initialize the logfile...
    #
    printf "Server: $JOB_SERVER\n" >$LOG_FILE
    printf "Job: $JOB_NAME started DATE:`now`\n" >>$LOG_FILE

    $ORACLE_HOME/bin/exp userid=system/${sidpwd} file=$ORA_EXP_FILE \
    log=$LOG_DIR/full_backup.log full=y

    RC=$?
    [ $RC != 0 ] &&cleanup $RC "Export wacarc"
    cleanup 0
    }更多精彩文章及讨论,请光临枫下论坛 rolia.net
    • 你是说执行了三次吗?是顺序移动啊。最旧的永远是第三个。
      • 不是,执行的结果是:
        /u06/export/ 是空
        /u06/export/day_1 是当天备份的文件
        /u06/export/day_2 是空
        /u06/export/day_3 是前一天备份的文件
        好像是mv 的部分执行了两次
        • 你的意思是不是
          mv /u06/export/day_2/fullexp_wacarc.dmp.Z /u06/export/day_3 这句话还没有执行完,已经把下面一句也执行了
          mv /u06/export/day_1/fullexp_wacarc.dmp.Z /u06/export/day_2
          然后看到的day_3是昨天的东西,应该是前天的东西,这样理解对吗?
          • 我也不知道是怎么回事,只是猜测. day_2应该是前天的备肥,day_1应该是昨天的备肥,/u06/export/是当天的备份,可是现在中间总有一个空目录,好像备份没完成时,程序由被运行了一次,另外output file如下,请进:
            Server: xxxxxxx
            Job: export_compressed started DATE:050415 (Fri) 04:00:00
            Job completed; return code 0 DATE:050415 (Fri) 04:00:10
            Job completed; return code 0 DATE:050415 (Fri) 04:00:13

            不明白为什么会有2行“Job completed“
            • 看着两句话,这个Shell不是你写的啊,我不小心看到了很多我想要的东西,我就是看的懂,懒得写。
              [ $RC != 0 ] &&cleanup $RC "Export wacarc"
              cleanup 0
              第一句话,就是if then else 的语句,你的RC执行出来的结果不是0,所以执行了一次cleanup,
              第二句话,就是再执行一次。
              所以你看到了两次结果。
              • 程序不是我写的。我只是不明白,如果第一次RC执行出来的结果不是0, 为什么输出文件中的两次return code 0 , 而不是“Batch Job Failure“
                • You need to study "korn shell" programming. It never give you "Batch job failure". The return "0" you see is the status of last executed cmd in that shell script.
              • 另外$RC的值是exp's return code 还是compress's return code? Thanks!
                • I did test on my UNIX box, it only returns me one time, either "Job completed" or "Batch Job Failure....."
                  • I did the test on our dev box, the result is same as yours. Even on prod server, it only occur at some night. not always. :-)
                    • then you need analyze the difference and narrow down the problem. have fun!
                • Let me answer this question. In shell script, the return code is always the last command it executed. The special case is using "exit <val>" to terminate the program, then the return code will be "<val>"
    • This is the aswer: You need to verbose debug your script. to see why "mv /u06/export/day_1/fullexp_wacarc.dmp.Z /u06/export/day_2" failed. This is the procedure:
      1) ksh -x <script name>, it will verbose all command line and its output.
      2) the second way to do it is to change the first line of script from
      #!/bin/ksh
      to
      #!/bin/ksh -x

      Good luck !