Bowen Peng
Bowen Peng

Reputation: 1825

How to duplicate shell command with date increasing

For example, I get a shell script as follows:

#!/bin/bash
hadoop fs -mkdir -p /user/hadoop-mining/SEARCH/2020-11-25/ | true

For the requirement, I have to repeat the date from 2020-11-25 to 2021-06-30 in that shell script.
AND it should be like this:

#!/bin/bash
hadoop fs -mkdir -p /user/hadoop-mining/SEARCH/2020-11-25/ | true
hadoop fs -mkdir -p /user/hadoop-mining/SEARCH/2020-11-26/ | true
hadoop fs -mkdir -p /user/hadoop-mining/SEARCH/2020-11-27/ | true
..., ...

hadoop fs -mkdir -p /user/hadoop-mining/SEARCH/2021-06-29/ | true
hadoop fs -mkdir -p /user/hadoop-mining/SEARCH/2021-06-30/ | true

SO could anyone help me and give me some hints.
Thanks in advance.

Upvotes: 1

Views: 68

Answers (3)

glenn jackman
glenn jackman

Reputation: 246837

With bash and GNU date

d=2020-11-25
end=2021-06-30
until [[ $d > $end ]]; do
    echo "hadoop fs -mkdir -p /user/hadoop-mining/SEARCH/$d/ | true" 
    d=$( date -d "$d + 1 day" "+%F" )
done >> script.sh

It would be more "natural" to write while [[ $d <= $end ]], but bash does not have a <= string comparison operator, so we have to negate the opposite operator.

Upvotes: 3

balderman
balderman

Reputation: 23815

If python can be used ...
Here is the solution

from datetime import datetime, timedelta

n = 1
day_count = 218

start_date = datetime(2020, 11, 25)
for single_date in (start_date + timedelta(n) for n in range(day_count)):
    print(f'hadoop fs -mkdir -p /user/hadoop-mining/SEARCH/{single_date.date()}/ | true')

Upvotes: 2

Ljm Dullaart
Ljm Dullaart

Reputation: 4969

As an example:

#!/bin/bash

i=0
while [ $i -lt 9 ] ; do
    i=$((i+1))
    dt=$(date -d"2020-11-25+$i days" +%Y-%m-%d)
    echo "/user/hadoop-mining/SEARCH/$dt"
done

Instead of echo, use your mkdir; instead of 9, use your own upper limit.

done

Upvotes: 1

Related Questions