ファイルから日付を読み取って変換する

ファイルから日付を読み取って変換する

私はCentOSを使用しており、シェルスクリプトを作成したいと思います。だから

日付を含むファイルがあります。

> 
> cat VM1_EOMAP_TIME.log
07 Sep 2022 16:30
> 

現在時刻以降の分とファイルの日付を取得したいと思います。

私の考えは次のとおりです。

でも「現在の時代の新起源」が得られない

> cat VM1_EOMAP_TIME.log
07 Sep 2022 16:30
 
> date --date='07 Sep 2022 16:30' +%s
1662568200
 
> date --date=$(cat VM1_EOMAP_TIME.log) +%s  
date: extra operand ‘2022’
Try 'date --help' for more information.

> date --date=`cat VM1_EOMAP_TIME.log` +%s
date: extra operand ‘2022’
Try 'date --help' for more information.

> TTT="07 Sep 2022 16:30"
> echo $TTT
07 Sep 2022 16:30

> date --date=$TTT +%s
date: extra operand ‘2022’
Try 'date --help' for more information.

「日付:追加オペランド「2022」」エラーが発生するのはなぜですか?

ベストアンサー1

bashが最近の場合、現在の時間(epoch秒)が組み込まれています。$EPOCHSECONDS

それ以外の場合は、bashのprintfは%(fmt)Tディレクティブと魔法値「-1」を使用してこれを取得できます。

printf -v current_epoch '%(%s)T' -1

また、bashには便利な機能が組み込まれています$(cat file)=> $(<file)

log_epoch=$( date -d "$(<VM1_EOMAP_TIME.log)" "+%s" )

おすすめ記事