How to convert this Python code into a shell script?

Viewed 50
import datetime   

def get_a_week(start) 
    date = datetime.datetime(start)
    dates = []
    for i in range(7):
        date += datetime.timedelta(days=1)
        print(date)
        dates.append(date)
    return dates

7_days = get_a_week('2022-09-01')

I need to periodically download files from remote machine, and part of the file name is a date, such as:

file_20220901

Each time I want to download 7 files for seven sequential days, so I need to use a logic like above to download files. How to covert this python code above into a shell script? It will download files using this command:

rsync x.x.x.x::user2/workspace/waic/tag/data/tag.20220901 data
1 Answers

You can do it using a for loop.

start="2022-09-02"
for i in {0..6}
do
  : $((d=$(date -d "$start + $i day" +"%Y%m%d")))
  echo "RANDOM_TEXT_$d"
  # you can replace the echo above by the command that you want.
done

Or in a POSIX shell:

start="2022-09-02"
: "$((i=1))"
while [ "$((i != 7))" -ne 0 ]
do
    : $((d=$(date -d "$start + $i day" +"%Y%m%d")))
    echo "RANDOM_TEXT_$d"
    : "$((i = i + 1))"
done

Job done :)

Note: on OS X the date command may be different.

Related