15
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Bashでの時間比較(指定した時刻を過ぎているかどうか判定する)

Last updated at Posted at 2018-12-20

はじめに

現在の時刻と、指定した時刻を比較して、指定した時刻を過ぎているかどうかを、bashで判定するスクリプトです。

Linuxでの時間比較

現在の時間をUNIX時間で表示(2018/12/20 22:38頃実行)

$ date +%s
1545313137

--dateオプションで、指定した時間をUNIX時間で表示

$ date --date "12/21 00:00" +%s
1545318000

この2つの値を比較する。


#!/bin/bash

now=`date +%s`
cmp=`date --date "12/21 00:00" +%s`

if [ $now -lt $cmp ]; then # -lt : '<'
        echo "まだ12/21日より前"
else
        echo "12/21を過ぎた"
fi
実行結果
まだ12/21日より前

終わりに

bash勉強しよう……。

15
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?