crontabエントリで毎週月曜日と木曜日に実行されるbashスクリプトがあります。
これには、以下のコードに従って1つまたは別のコードを実行する条件があります。
if [ "$(LC_TIME=C date +%a)" == "Thu" ] && [ "$(date +%d)" -ge 15 ] &&
[ "$(date +%d)" -le 21 ] && [[ "$(LC_TIME=C date +%b)" == "Feb" || "$(date +%b)" == "Apr" || "$(date +%b)" == "Jun" || "$(date +%b)" == "Aug" || "$(date +%b)" == "Oct" || "$(date +%b)" == "Dec" ]]
then
// condition a
else
// condition b
fi
上記の内容を最もよくテストする方法がわからず、bashにあまり自信がないので、「条件a」が2月、4月など毎週第3木曜日に実行されると予想します。そうでなければ、「条件b」が実行される。
よろしくお願いします。
ベストアンサー1
一度だけ電話してくださいdate
。私は次のように書きます:
# ask date for the weekday, date and month: store into variables
read -r day date month < <( LC_TIME=C date "+%a %_d %b" )
# the 3rd Thu of the month has date between 15 and 21 inclusive
if [[ $day == "Thu" ]] &&
(( 15 <= date && date <= 21 )) &&
[[ ":Feb:Apr:Jun:Aug:Oct:Dec:" == *:"$month":* ]]
then
echo "code a"
else
echo "code b"
fi
これはbash==
演算子に依存します。パターンマッチングは文字列恒等演算子ではありません。