How to loop through dates using Bash?

后端 未结 8 731
一整个雨季
一整个雨季 2020-12-02 06:22

I have such bash script:

array=( \'2015-01-01\', \'2015-01-02\' )

for i in \"${array[@]}\"
do
    python /home/user/executeJobs.py {i} &> /home/user/         


        
8条回答
  •  情深已故
    2020-12-02 06:37

    I needed to loop through dates on AIX, BSDs, Linux, OS X and Solaris. The date command is one of the least portable and most miserable commands to use across platforms I have encountered. I found it easier to write a my_date command that just worked everywhere.

    The C program below takes a starting date, and adds or subtracts days from it. If no date is supplied, it adds or subtracts days from the current date.

    The my_date command allows you to perform the following everywhere:

    start="2015-01-01"
    stop="2015-01-31"
    
    echo "Iterating dates from ${start} to ${stop}."
    
    while [[ "${start}" != "${stop}" ]]
    do
        python /home/user/executeJobs.py {i} &> "/home/user/${start}.log"
        start=$(my_date -s "${start}" -n +1)
    done
    

    And the C code:

    #include 
    #include 
    #include 
    #include 
    
    int show_help();
    
    int main(int argc, char* argv[])
    {
        int eol = 0, help = 0, n_days = 0;
        int ret = EXIT_FAILURE;
    
        time_t startDate = time(NULL);
        const time_t ONE_DAY = 24 * 60 * 60;
    
        for (int i=0; i

提交回复
热议问题