🖥

Linux—シェルスクリプトで日付でループする (Mac対応あり)

2023/08/26に公開
date_loop.sh
#!/bin/bash -eu

#
# YYYY-MM-DD
#
readonly start_date=2014-02-20
readonly end_date=2014-03-02

# For mac OS
#
# You need before run this script
# $ brew install coreutils
#
# shopt -s expand_aliases
# alias date=gdate

current_date="$start_date"
while true; do

        # 処理
        echo "$current_date"

        if [ "$current_date" = "$end_date" ] ; then
                break
        fi

        current_date=$(date -d "$current_date 1day" "+%Y-%m-%d")
done

結果

2014-02-20
2014-02-21
2014-02-22
2014-02-23
2014-02-24
2014-02-25
2014-02-26
2014-02-27
2014-02-28
2014-03-01
2014-03-02

バージョン

GNU bash, version 4.4.0(1)-release (x86_64-apple-darwin15.6.0)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ

Twitter

https://twitter.com/YumaInaura

公開日時

2018-08-10

Discussion