如何检查今天是否是周末狂欢? [英] How to check if today is a weekend in bash?
本文介绍了如何检查今天是否是周末狂欢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何查看今天是周末还是bash甚至perl?
How to check if today is a weekend using bash or even perl?
我想阻止某些程序在周末运行.
I want to prevent certain programs to run on a weekend.
推荐答案
您可以使用类似以下内容的
You can use something like:
if [[ $(date +%u) -gt 5 ]]; then echo weekend; fi
date +%u
为您提供从星期一(1)到星期日(7)的星期几.如果大于5(周六为6,周日为7),则为周末.
date +%u
gives you the day of the week from Monday (1) through to Sunday (7). If it's greater than 5 (Saturday is 6 and Sunday is 7), then it's the weekend.
因此您可以在脚本顶部放置以下内容:
So you could put something like this at the top of your script:
if [[ $(date +%u) -gt 5 ]]; then
echo 'Sorry, you cannot run this program on the weekend.'
exit
fi
或更简洁:
[[ $(date +%u) -gt 5 ]] && { echo "Weekend, not running"; exit; }
要检查是否为工作日,请使用相反的含义(< 6
而不是> 5
):
To check if it's a weekday, use the opposite sense (< 6
rather than > 5
):
$(date +%u) -lt 6
这篇关于如何检查今天是否是周末狂欢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文