0

有人可以帮助我理解以下代码,它决定了从数据库中挑选数据的开始日期和结束日期。

# Get the current time as the stop time.
#
stoptime=`date +"%Y-%m-%d %H:00"`
if test $? -ne 0
then
   echo "Failed to get the date"
   rm -f $1/.optpamo.pid
   exit 4
fi

#
# Read the lasttime file to get the start time
#
if test -f $1/optlasttime
then
   starttime=`cat $1/optlasttime`
   # if the length of the chain is zero
   # (lasttime is empty) It is updated properly
   # (and I wait for the following hour)
   if test -z "$starttime"
   then
      echo "Empty file lasttime"
      echo $stoptime > $1/optlasttime
      rm -f $1/.optpamo.pid
      exit 5
   fi
else
   # If lasttime does not exist I create, it with the present date
   # and I wait for the following hour
   echo "File lasttime does not exist"
   echo $stoptime > $1/optlasttime
   rm -f $1/.optpamo.pid
   exit 6
fi

谢谢

4

3 回答 3

1

optlasttime该脚本检查在指定为参数 ( $1)的目录中是否存在命名的非空文件。如果是这样,则脚本成功退出(状态0)。如果文件不存在或为空,则将格式化为的当前小时2010-01-07 14:00写入文件,.optpamo.pid从参数目录中删除另一个名为的文件,并且脚本不成功退出(状态56)。

这个脚本显然是一个被某个外部进程调用的实用程序,您需要参考它才能完全理解。

于 2010-01-07T19:26:56.117 回答
0

我将其中的一小段复制并粘贴到我调用的文件中test.ksh

stoptime=`date +"%Y-%m-%d %H:00"`
if test $? -ne 0
then
   echo "Failed to get the date"
   rm -f $1/.optpamo.pid
   exit 4
fi

然后我在命令行运行它,如下所示:

zhasper@berens:~$ ksh -x ./temp.ksh 
+ date '+%Y-%m-%d %H:00'
+ stoptime='2010-01-08 18:00'
+ test 0 -ne 0

ksh的-x标志使它在执行时完整地打印出每个命令行。将您在此处看到的内容与上面的 shell 脚本片段进行比较,您应该可以了解 ksh 是如何解释文件的。

如果你在整个文件上运行它,你应该对它在做什么有一个很好的感觉。

要了解更多,您可以阅读man ksh,或在线搜索ksh scripting tutorial

与我们简单地告诉您脚本的作用相比,这三件事一起应该可以帮助您了解更多。

于 2010-01-08T07:10:49.800 回答
0

1.) 将停止时间设置为当前时间

2.) 检查文件 $1/optlasttime 是否存在(其中 $1 被传递到脚本中)

 a.) if $1/optlasttime exists it checks the contents of the file (which it is assumed that if it does have contents it is a timestamp)

 b.) if $1/optlasttime does not exist it populates the $1/optlasttime file with the stoptime. 
于 2010-01-07T19:53:37.220 回答