开发者

understanding a ksh script part of

Could someone help me understand the following piece of code which is deciding on t开发者_如何学运维he start and end dates to pick data out of a db.

# 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

Thanks


The script checks to see if there's a non-empty file named optlasttime in the directory specified as an argument ($1). If so, the script exits successfully (status 0). If the file doesn't exist or is empty, the current hour formatted as 2010-01-07 14:00 is written to the file, another file named .optpamo.pid is deleted from the argument directory and the script exits unsuccessfully (status 5 or 6).

This script is obviously a utility being called by some outer process, to which you need to refer for full understanding.


1.) Sets stop time to current time

2.) Checks if file $1/optlasttime exists (where $1 is passed into the script)

 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. 


I copied and pasted a small snippet of this into a file I called 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

Then I ran it at the commandline, like so:

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

The -x flag to ksh makes it print out each commandline, in full, as it executes. Comparing what you see here with the snippet of shell script above should tell you something about how ksh is interpreting the file.

If you run this over the whole file you should get a good feel for what it's doing.

To learn more, you can read man ksh, or search for ksh scripting tutorial online.

Together, these three things should help you learn a lot more than us simply telling you what the script does.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜