Created
February 8, 2022 06:37
-
-
Save Comamoca/b090a327f25275d0689f72d9020ce25a to your computer and use it in GitHub Desktop.
時刻に合わせて表示内容が変わるシェルスクリプトのサンプル(解説付き)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# bash ./time_echo.sh | |
# 処理の解説 | |
# 現在時刻をコロン無し24時間表記で扱っている | |
# 例) 午後1時 -> 1300 | |
# 午前7時 -> 0700 | |
# 条件式に出てくる-ltなどはtestコマンドのオプション | |
# つまり[]はtestコマンドと同じ役割をしている | |
# ここにtestコマンドで使える比較オプションをまとめる | |
#test コマンドの数値比較条件一覧表 | |
#オプション 使用例 オプションの意味 数式で表すと… | |
#-eq test num1 -eq num2 num1 と num2 が等しければ真となる。 num1=num2 | |
#-ne test num1 -ne num2 num1 と num2 が等しくなければ真となる。 num1≠num2 | |
#-lt test num1 -lt num2 num1 が num2 より小ならば真となる。 num1<num2 | |
#-le test num1 -le num2 num1 が num2 以下ならば真となる。 num1≦num2 | |
#-gt test num1 -gt num2 num1 が num2 より大ならば真となる。 num1>num2 | |
#-ge test num1 -ge num2 num1 が num2 以上ならば真となる。 num1≧num2 | |
# 条件式の間にある&&は論理演算子のANDで、これを挟む事によって〜かつ〜という処理が実現できる。 | |
# [ $now -ge 0700 ] && [ $now -le 0900 ]; | |
# この場合は、0700より大きく(-ge)、かつ0900より小さい(-le)場合に処理が実行される。 | |
# ===== 参考サイト ===== | |
# Bashのif文でANDやOR条件、&&や||演算子を使う | |
# https://tex2e.github.io/blog/shell/bash-and-or | |
# if 文と test コマンド | |
# https://shellscript.sunone.me/if_and_test.html | |
# シェルスクリプトで現在の時刻(hh:mm)を取得してif文で分岐 | |
# https://ja.stackoverflow.com/questions/61223/%E3%82%B7%E3%82%A7%E3%83%AB%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88%E3%81%A7%E7%8F%BE%E5%9C%A8%E3%81%AE%E6%99%82%E5%88%BBhhmm%E3%82%92%E5%8F%96%E5%BE%97%E3%81%97%E3%81%A6if%E6%96%87%E3%81%A7%E5%88%86%E5%B2%90 | |
# =========================================================================================================== | |
now=$(date '+%H%M') | |
echo "現在時刻: "$now | |
# if [ $now -ge $start_time -a $now -lt $finish_time ]; then | |
# echo 処理開始 | |
# fi | |
if [ $now -ge 0700 ] && [ $now -le 0900 ]; then | |
echo "おは🌽!" | |
elif [ $now -ge 1100 ] && [ $now -le 1300 ]; then | |
echo "ひる🌽!" | |
elif [ $now -ge 1300 ] && [ $now -le 1800 ]; then | |
echo "にゃっはろ〜🌸" | |
elif [ $now -ge 1800 ] && [ $now -le 2300 ]; then | |
echo "🌽ばんは〜!" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment