How to check if today is a weekend in bash?

前端 未结 7 1626
悲哀的现实
悲哀的现实 2020-12-25 11:41

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

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

相关标签:
7条回答
  • 2020-12-25 12:25

    Use Perl's localtime operator.

    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);
    

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

    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
    0 讨论(0)
提交回复
热议问题