Manipulate date in UNIX

后端 未结 2 459
甜味超标
甜味超标 2021-01-23 09:16

I want to find a way to list all the dates of the current week in UNIX. For example if today is 17-07-2018 then how to get dates 16-07, 18-07, 19-07 and so on for the whole week

2条回答
  •  感情败类
    2021-01-23 09:50

    Well, now I think it requires GNU!date or some actual programming language. Perl, for instance.

    #!/usr/bin/perl
    
    use strict;
    use POSIX;
    
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);
    my ($w,$ts);
    
    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime();
    
    $ENV{'TZ'}='GMT';
    POSIX::tzset ();
    
    $ts = POSIX::mktime (0, 0, 0, $mday, $mon, $year);
    $ts = $ts - 24*60*60 * $wday;
    
    for ($w= 1; $w<=7; ++$w) {
        ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
            gmtime($ts + (24*60*60)*$w);
        printf ("%04d-%02d-%02dn", $year+1900, $mon+1, $mday);
    }
    

    If you save it as $HOME/bin/weekdays.pl then you can use it in your scripts:

    set $(perl ~/bin/weekdays.pl)
    echo "Monday is $1"
    

提交回复
热议问题