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。その場合、スクリプトは正常に終了します(status 0)。ファイルが存在しないか空の場合、現在の時間2010-01-07 14:00はファイルに書き込まれるとおりにフォーマットされ、名前の付いた別のファイル.optpamo.pidが引数ディレクトリから削除され、スクリプトが正常に終了しません(status5または6)。

このスクリプトは明らかに、完全に理解するために参照する必要がある外部プロセスによって呼び出されるユーティリティです。

于 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

-xkshのフラグにより​​、実行時に各コマンドラインが完全に出力されます。ここに表示されているものを上記のシェルスクリプトのスニペットと比較すると、kshがファイルをどのように解釈しているかがわかります。

これをファイル全体で実行すると、ファイルが何をしているのかをよく理解できるはずです。

詳細については、オンラインで読むman kshか、検索してksh scripting tutorialください。

これらの3つのことを合わせると、スクリプトの機能を説明するだけでなく、多くのことを学ぶことができます。

于 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 に答える