date コマンドは、与えられた変数に基づいて月前を計算します。

date コマンドは、与えられた変数に基づいて月前を計算します。

パラメータなしで現在の月と年を生成できます(012021)と先月(122020) "-d '先月'"オプションを使用します。 「のようなものを入力すると2019年1月1日"$currentmonth( を処理できます。012019)しかし、$ pastmonth変数ではありません。この問題を解決する方法はありますか?

#!/bin/bash
if [ -z "$1" ] ;then
currentmonth=`date "+%m%Y"`
pastmonth=`date "+%m%Y" -d 'last month'`
echo $currentmonth
echo $pastmonth
else
currentmonth=`date +%m%Y --date="$1"`
pastmonth=?
echo $currentmonth
echo $pastmonth
fi

ベストアンサー1

日付に「先月」を「追加」するだけです。

date +%m%Y -d "01/01/2019+last month"

気をつけるdate分析、スラッシュのある日付に米国形式を使用します。

おすすめ記事