bash:2つのタイムスタンプ間の期間は1時間より長くなりますか?

bash:2つのタイムスタンプ間の期間は1時間より長くなりますか?

私はCentOSで実行されるbashスクリプトを書いています。このスクリプトは、最初にアプリケーションセッションが開始して終了する行を取得し、1時間以上続くと出力します。

ログファイルのタイムスタンプ形式は2017-01-03T00:00:15.529596-03:00です。

$ iはアプリケーションセッションIDです。


これが私が今まで持っているものです:

for i in $(  grep 'session-enter\|session-exit' logfile | awk '{ print $5}' ); do
                        echo ""
                        echo "*****   $i   *****"
        grep 'session-enter\|session-exit' logfile | grep $i

                start=$(grep session-enter logfile | grep $i | awk '{ print $1 }' | sed 's/-03:00//g')
                end=$(grep session-exit logfile | grep $i | awk '{ print $1 }' | sed 's/-03:00//g')

                epochStart=$(date -d "$start" +%s )
                epochEnd=$(date -d "$end" +%s )

                                duration=$( date -u -d "0 $epochEnd seconds - $epochStart seconds" +"%H:%M:%S" )

                        if [ "$epochStart"="" ] || [ "$epochEnd"="" ]
                                then
                                     echo Duration: $duration
                                else
                                     continue
                        fi


done

これにご協力いただきありがとうございます。

ベストアンサー1

エポック以降の開始時刻と終了時刻を秒単位で保存したようです。これが本当なら、次のことができるはずです。

duration=$(( $epochEnd - $epochStart ))
[ $duration -gt 3600 ] && echo "Duration: $duration"

おすすめ記事