bash 如何在bash中检查今天是否是周末?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3490032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 19:35:16  来源:igfitidea点击:

How to check if today is a weekend in bash?

perlbashscripting

提问by vehomzzz

How to check if today is a weekend using bash or even perl?

如何使用bash甚至perl检查今天是否是周末?

I want to prevent certain programs to run on a weekend.

我想阻止某些程序在周末运行。

回答by paxdiablo

You can use something like:

你可以使用类似的东西:

if [[ $(date +%u) -gt 5 ]]; then echo weekend; fi

date +%ugives 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.

date +%u为您提供从星期一 (1) 到星期日 (7) 的星期几。如果它大于 5(星期六是 6,星期天是 7),那么它就是周末。

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

Or the more succinct:

或者更简洁的:

[[ $(date +%u) -gt 5 ]] && { echo "Weekend, not running"; exit; }

To check if it's a weekday,use the opposite sense (< 6rather than > 5):

要检查它是否是工作日,请使用相反的含义(< 6而不是> 5):

$(date +%u) -lt 6

回答by ghostdog74

case "$(date +%a)" in 
  Sat|Sun) echo "weekend";;
esac

回答by Michael Aaron Safyan

This is actually a surprisingly difficult problem, because who is to say that "weekend" means Saturday and Sunday... what constitutes "the weekend" can actually vary across cultures (e.g. in Israel, people work on Sunday and have Friday off). While you can get the date with the datecommand, you will need to store some additional data indicating what constitutes the weekend for each locale if you are to implement this in a way that works for all users. If you target only one country, then the solution posed in the other answers will work... but it is always good to keep in mind the assumptions being made here.

这实际上是一个令人惊讶的难题,因为谁能说“周末”意味着周六和周日……“周末”的构成实际上因文化而异(例如,在以色列,人们周日工作,周五休息)。虽然您可以使用该date命令获取日期,但如果您要以适用于所有用户的方式实现这一点,您将需要存储一些额外的数据来指示每个语言环境的周末是什么。如果您只针对一个国家/地区,那么其他答案中提出的解决方案将有效……但记住这里所做的假设总是好的。

回答by Greg Bacon

Use Perl's localtimeoperator.

使用 Perl 的localtime运算符。

localtime

Converts a time as returned by the time function to a 9-element list with the time analyzed for the local time zone. Typically used as follows:

#  0    1    2     3     4    5     6     7     8
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);

$wdayis the day of the week, with 0 indicating Sunday and 3 indicating Wednesday.

当地时间

将 time 函数返回的时间转换为包含本地时区分析时间的 9 元素列表。通常使用如下:

#  0    1    2     3     4    5     6     7     8
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);

$wday是星期几,0 表示星期日,3 表示星期三。

For example:

例如:

$ date
Sun Aug 15 20:27:29 CDT 2010

$ perl -le 'my $wday = (localtime)[6];
            print $wday >= 1 && $wday <= 5 ? "weekday" : "weekend"'
weekend

回答by Ivan

printf also has date

printf 也有日期

printf -v day '%(%a)T'
case $day in 
    Sat|Sun) echo "Hooray!";;
esac

https://ideone.com/wU7C0c- demo

https://ideone.com/wU7C0c- 演示

回答by Chris

I am not really sure if this is suited to this question but I wanted to share this it to help others out and this is the closest Stack question to what I was looking for.

我不确定这是否适合这个问题,但我想分享它以帮助其他人,这是与我正在寻找的最接近的 Stack 问题。

This is a shell script that I use for starting an application when i boot my computer, the application cannot connect to its server over the weekend (its down for maintenance as the service doesn't run at weekends (forex trading application).

这是一个 shell 脚本,我在启动计算机时用于启动应用程序,该应用程序在周末无法连接到其服务器(由于服务不在周末运行(外汇交易应用程序)而停机进行维护)。

Information about this script, d is 1 to 7 (mon to sun) h is 24 time. You can adjust this to your own settings, maybe you have a midweek maintenance or any other cause. This is my first ever shell script, so I'm sure there is a better way and if anybody wants make edits, feel free I will review for acceptance (or somebody else may do it)

关于这个脚本的信息,d 是 1 到 7(mon 到 sun)h 是 24 时间。您可以将其调整为您自己的设置,也许您有周中维护或任何其他原因。这是我的第一个 shell 脚本,所以我确信有更好的方法,如果有人想进行编辑,请随时查看我是否接受(或其他人可能会这样做)

#!/bin/bash
d=$(date +%u)
h=$(date +%H)
case $d in
    '5')
    # Friday after 10pm
    if (($h >= 22)) ; then  
        exit
    fi
    ;;
    '6')
    # all day saturday
        exit
    ;;
    '7')
    #sunday before 10pm
    if (($h < 22)) ; then  
        exit
    fi
    ;;
    *)
    # any other time run the program

    ;;
esac

回答by Eugene Ionichev

I like this way of running programs on weekdays:

我喜欢这种平日运行程序的方式:

[[ $(date +%u) -lt 6 ]] && weekday-program

It is a bit tricky construction, but compact and easy to add.

这是一个有点棘手的结构,但紧凑且易于添加。