How to loop through dates using Bash?

后端 未结 8 747
一整个雨季
一整个雨季 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:50

    I had the same issue and I tried some of the above answers, maybe they are ok, but none of those answers fixed on what I was trying to do, using macOS.

    I was trying to iterate over dates in the past, and the following is what worked for me:

    #!/bin/bash
    
    # Get the machine date
    newDate=$(date '+%m-%d-%y')
    
    # Set a counter variable
    counter=1 
    
    # Increase the counter to get back in time
    while [ "$newDate" != 06-01-18 ]; do
      echo $newDate
      newDate=$(date -v -${counter}d '+%m-%d-%y')
      counter=$((counter + 1))
    done
    

    Hope it helps.

提交回复
热议问题