Date & Time loop in bash
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I am trying to download the yesterday logs through an external API, For API I need to provide a date range from & to as input parameters.
From API I can download 1 hour log max for each call.
So if I want to download yesterday complete log. I need to call the API for 24 times with yesterday date(every hour time).
Note API will support only GMT time only. So need to provide GMT time.
Eg :- https://abcd.com/logs?start=FROM_DATE&end=TO_DATE
First time loop it should go like below,
https://abcd.com/logs?start=29-04-2018T00:00:00Z&end=29-04-2018T00:59:59Z
Second time loop it should go like below
https://abcd.com/logs?start=29-04-2018T01:00:00Z&end=29-04-2018T01:59:59Z
Last time loop like below,
https://abcd.com/logs?start=29-04-2018T23:00:00Z&end=29-04-2018T23:59:59Z
This script i will add in cron schedule, so everyday once it will get triggered and download yesterday 24 hour log complete.
Key points are
- Yesterday date always
- Format GMT
- 24 times loop
Hope it is clear. The below mentioned answers are throwing error. I started with this script, it is considering the current time and doing the loop. Instead of that loop needs to start with 00:00:00 GMT time. Someone please help me.
#!/bin/bash
FROM_DATE=$(date -u -d "1 day ago" +%Y-%m-%d"T"%H":00:00""Z")
for i in 0..3
do
echo "FROM_DATE : $FROM_DATE"
TO_DATE=$(date -u +%Y-%m-%d"T"%H":59:59""Z" -d "1 day ago""$date + $i hour")
echo "TO_DATE : $TO_DATE"
FROM_DATE=$(date -u +%Y-%m-%d"T"%H":00:00""Z" -d "1 day ago""$date + $i hour""$date + 1 hour")
done
bash shell-script shell bashrc
add a comment |Â
up vote
1
down vote
favorite
I am trying to download the yesterday logs through an external API, For API I need to provide a date range from & to as input parameters.
From API I can download 1 hour log max for each call.
So if I want to download yesterday complete log. I need to call the API for 24 times with yesterday date(every hour time).
Note API will support only GMT time only. So need to provide GMT time.
Eg :- https://abcd.com/logs?start=FROM_DATE&end=TO_DATE
First time loop it should go like below,
https://abcd.com/logs?start=29-04-2018T00:00:00Z&end=29-04-2018T00:59:59Z
Second time loop it should go like below
https://abcd.com/logs?start=29-04-2018T01:00:00Z&end=29-04-2018T01:59:59Z
Last time loop like below,
https://abcd.com/logs?start=29-04-2018T23:00:00Z&end=29-04-2018T23:59:59Z
This script i will add in cron schedule, so everyday once it will get triggered and download yesterday 24 hour log complete.
Key points are
- Yesterday date always
- Format GMT
- 24 times loop
Hope it is clear. The below mentioned answers are throwing error. I started with this script, it is considering the current time and doing the loop. Instead of that loop needs to start with 00:00:00 GMT time. Someone please help me.
#!/bin/bash
FROM_DATE=$(date -u -d "1 day ago" +%Y-%m-%d"T"%H":00:00""Z")
for i in 0..3
do
echo "FROM_DATE : $FROM_DATE"
TO_DATE=$(date -u +%Y-%m-%d"T"%H":59:59""Z" -d "1 day ago""$date + $i hour")
echo "TO_DATE : $TO_DATE"
FROM_DATE=$(date -u +%Y-%m-%d"T"%H":00:00""Z" -d "1 day ago""$date + $i hour""$date + 1 hour")
done
bash shell-script shell bashrc
it is considering the current time and doing the loop. Instead of that loop needs to start with 00:00:00 GMT time
â Jhony
Apr 27 at 12:12
Hard to read a script in a comment. I added it to the question, but please verify it. Something seems to have been eaten in the comment, so edit it to correct it.
â Philippos
Apr 27 at 12:44
that is correct. I took hour and appended the minutes and seconds.
â Jhony
Apr 27 at 13:09
Problem is my loop needs to start with GMT 00:00:00 instead of considering current hour. Please help it
â Jhony
Apr 27 at 13:10
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am trying to download the yesterday logs through an external API, For API I need to provide a date range from & to as input parameters.
From API I can download 1 hour log max for each call.
So if I want to download yesterday complete log. I need to call the API for 24 times with yesterday date(every hour time).
Note API will support only GMT time only. So need to provide GMT time.
Eg :- https://abcd.com/logs?start=FROM_DATE&end=TO_DATE
First time loop it should go like below,
https://abcd.com/logs?start=29-04-2018T00:00:00Z&end=29-04-2018T00:59:59Z
Second time loop it should go like below
https://abcd.com/logs?start=29-04-2018T01:00:00Z&end=29-04-2018T01:59:59Z
Last time loop like below,
https://abcd.com/logs?start=29-04-2018T23:00:00Z&end=29-04-2018T23:59:59Z
This script i will add in cron schedule, so everyday once it will get triggered and download yesterday 24 hour log complete.
Key points are
- Yesterday date always
- Format GMT
- 24 times loop
Hope it is clear. The below mentioned answers are throwing error. I started with this script, it is considering the current time and doing the loop. Instead of that loop needs to start with 00:00:00 GMT time. Someone please help me.
#!/bin/bash
FROM_DATE=$(date -u -d "1 day ago" +%Y-%m-%d"T"%H":00:00""Z")
for i in 0..3
do
echo "FROM_DATE : $FROM_DATE"
TO_DATE=$(date -u +%Y-%m-%d"T"%H":59:59""Z" -d "1 day ago""$date + $i hour")
echo "TO_DATE : $TO_DATE"
FROM_DATE=$(date -u +%Y-%m-%d"T"%H":00:00""Z" -d "1 day ago""$date + $i hour""$date + 1 hour")
done
bash shell-script shell bashrc
I am trying to download the yesterday logs through an external API, For API I need to provide a date range from & to as input parameters.
From API I can download 1 hour log max for each call.
So if I want to download yesterday complete log. I need to call the API for 24 times with yesterday date(every hour time).
Note API will support only GMT time only. So need to provide GMT time.
Eg :- https://abcd.com/logs?start=FROM_DATE&end=TO_DATE
First time loop it should go like below,
https://abcd.com/logs?start=29-04-2018T00:00:00Z&end=29-04-2018T00:59:59Z
Second time loop it should go like below
https://abcd.com/logs?start=29-04-2018T01:00:00Z&end=29-04-2018T01:59:59Z
Last time loop like below,
https://abcd.com/logs?start=29-04-2018T23:00:00Z&end=29-04-2018T23:59:59Z
This script i will add in cron schedule, so everyday once it will get triggered and download yesterday 24 hour log complete.
Key points are
- Yesterday date always
- Format GMT
- 24 times loop
Hope it is clear. The below mentioned answers are throwing error. I started with this script, it is considering the current time and doing the loop. Instead of that loop needs to start with 00:00:00 GMT time. Someone please help me.
#!/bin/bash
FROM_DATE=$(date -u -d "1 day ago" +%Y-%m-%d"T"%H":00:00""Z")
for i in 0..3
do
echo "FROM_DATE : $FROM_DATE"
TO_DATE=$(date -u +%Y-%m-%d"T"%H":59:59""Z" -d "1 day ago""$date + $i hour")
echo "TO_DATE : $TO_DATE"
FROM_DATE=$(date -u +%Y-%m-%d"T"%H":00:00""Z" -d "1 day ago""$date + $i hour""$date + 1 hour")
done
bash shell-script shell bashrc
edited Apr 30 at 7:09
asked Apr 27 at 12:05
Jhony
144
144
it is considering the current time and doing the loop. Instead of that loop needs to start with 00:00:00 GMT time
â Jhony
Apr 27 at 12:12
Hard to read a script in a comment. I added it to the question, but please verify it. Something seems to have been eaten in the comment, so edit it to correct it.
â Philippos
Apr 27 at 12:44
that is correct. I took hour and appended the minutes and seconds.
â Jhony
Apr 27 at 13:09
Problem is my loop needs to start with GMT 00:00:00 instead of considering current hour. Please help it
â Jhony
Apr 27 at 13:10
add a comment |Â
it is considering the current time and doing the loop. Instead of that loop needs to start with 00:00:00 GMT time
â Jhony
Apr 27 at 12:12
Hard to read a script in a comment. I added it to the question, but please verify it. Something seems to have been eaten in the comment, so edit it to correct it.
â Philippos
Apr 27 at 12:44
that is correct. I took hour and appended the minutes and seconds.
â Jhony
Apr 27 at 13:09
Problem is my loop needs to start with GMT 00:00:00 instead of considering current hour. Please help it
â Jhony
Apr 27 at 13:10
it is considering the current time and doing the loop. Instead of that loop needs to start with 00:00:00 GMT time
â Jhony
Apr 27 at 12:12
it is considering the current time and doing the loop. Instead of that loop needs to start with 00:00:00 GMT time
â Jhony
Apr 27 at 12:12
Hard to read a script in a comment. I added it to the question, but please verify it. Something seems to have been eaten in the comment, so edit it to correct it.
â Philippos
Apr 27 at 12:44
Hard to read a script in a comment. I added it to the question, but please verify it. Something seems to have been eaten in the comment, so edit it to correct it.
â Philippos
Apr 27 at 12:44
that is correct. I took hour and appended the minutes and seconds.
â Jhony
Apr 27 at 13:09
that is correct. I took hour and appended the minutes and seconds.
â Jhony
Apr 27 at 13:09
Problem is my loop needs to start with GMT 00:00:00 instead of considering current hour. Please help it
â Jhony
Apr 27 at 13:10
Problem is my loop needs to start with GMT 00:00:00 instead of considering current hour. Please help it
â Jhony
Apr 27 at 13:10
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
1
down vote
Try this.
#!/bin/bash
#
yesterday=$(date --utc --date 'yesterday' +'%Y-%m-%d')
for hour in 0..23
do
printf "https://abcd.com/logs?start=%sT%02d:00:00Z&end=%sT%02d:59:59Zn" $yesterday $hour $yesterday $hour
done
It won't handle the occasional leap seconds. If you need that, use this more complex code where the last range of the day may need to go from 23:00:00
to 23:59:60
, but ensure that your target application can also handle this:
#!/bin/bash
#
yesterday=$(date --utc --date 'yesterday' +'%Y-%m-%d')
leapsecond=$(date --utc --date @$(( $(date --utc --date '00:00:00' +%s) -1 )) +'%S')
lastsecond=59
for hour in 0..23
do
[[ hour == 23 ]] && lastsecond=$leapsecond
printf "https://abcd.com/logs?start=%sT%02d:00:00Z&end=%sT%02d:59:%02dZn" $yesterday $hour $yesterday $hour $lastsecond
done
the out put of the first code is like below : invalid number printf: 0 : invalid number printf: 0 T00:59:59Z), TO_DATE(2018-04-26
â Jhony
Apr 27 at 13:17
Like what below? Please EDIT YOUR QUESTION to show us what you actually want, rather than expecting us to read your mind or try and interpret poorly formatted comments.
â roaima
Apr 27 at 13:18
i have just modified printf to echo it is coming like this 0018-04-26sT%02d:00:00Z), TO_DATE(%sT%02d:59:59Z)n 2018-04-26
â Jhony
Apr 27 at 13:18
@Jhony don't useecho
where I've usedprintf
. They're different commands for a reason.
â roaima
Apr 27 at 13:18
i have created script file with the above code as test.sh and running in linux environment
â Jhony
Apr 27 at 13:19
 |Â
show 11 more comments
up vote
0
down vote
I am getting as expected output.
#!/bin/bash
for i in 00..23
do FROM_DATE=$(date -u -d "1 day ago" +%Y-%m-%d"T"$i":00:00Z")
echo "FROM_DATE:" $FROM_DATE | tr -d 'r'
TO_DATE=$(date -u +%Y-%m-%d"T"$i":59:59Z" -d "1 day ago")
echo "TO_DATE : $TO_DATE" | tr -d 'r'
done
Thanks a lot for your help
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Try this.
#!/bin/bash
#
yesterday=$(date --utc --date 'yesterday' +'%Y-%m-%d')
for hour in 0..23
do
printf "https://abcd.com/logs?start=%sT%02d:00:00Z&end=%sT%02d:59:59Zn" $yesterday $hour $yesterday $hour
done
It won't handle the occasional leap seconds. If you need that, use this more complex code where the last range of the day may need to go from 23:00:00
to 23:59:60
, but ensure that your target application can also handle this:
#!/bin/bash
#
yesterday=$(date --utc --date 'yesterday' +'%Y-%m-%d')
leapsecond=$(date --utc --date @$(( $(date --utc --date '00:00:00' +%s) -1 )) +'%S')
lastsecond=59
for hour in 0..23
do
[[ hour == 23 ]] && lastsecond=$leapsecond
printf "https://abcd.com/logs?start=%sT%02d:00:00Z&end=%sT%02d:59:%02dZn" $yesterday $hour $yesterday $hour $lastsecond
done
the out put of the first code is like below : invalid number printf: 0 : invalid number printf: 0 T00:59:59Z), TO_DATE(2018-04-26
â Jhony
Apr 27 at 13:17
Like what below? Please EDIT YOUR QUESTION to show us what you actually want, rather than expecting us to read your mind or try and interpret poorly formatted comments.
â roaima
Apr 27 at 13:18
i have just modified printf to echo it is coming like this 0018-04-26sT%02d:00:00Z), TO_DATE(%sT%02d:59:59Z)n 2018-04-26
â Jhony
Apr 27 at 13:18
@Jhony don't useecho
where I've usedprintf
. They're different commands for a reason.
â roaima
Apr 27 at 13:18
i have created script file with the above code as test.sh and running in linux environment
â Jhony
Apr 27 at 13:19
 |Â
show 11 more comments
up vote
1
down vote
Try this.
#!/bin/bash
#
yesterday=$(date --utc --date 'yesterday' +'%Y-%m-%d')
for hour in 0..23
do
printf "https://abcd.com/logs?start=%sT%02d:00:00Z&end=%sT%02d:59:59Zn" $yesterday $hour $yesterday $hour
done
It won't handle the occasional leap seconds. If you need that, use this more complex code where the last range of the day may need to go from 23:00:00
to 23:59:60
, but ensure that your target application can also handle this:
#!/bin/bash
#
yesterday=$(date --utc --date 'yesterday' +'%Y-%m-%d')
leapsecond=$(date --utc --date @$(( $(date --utc --date '00:00:00' +%s) -1 )) +'%S')
lastsecond=59
for hour in 0..23
do
[[ hour == 23 ]] && lastsecond=$leapsecond
printf "https://abcd.com/logs?start=%sT%02d:00:00Z&end=%sT%02d:59:%02dZn" $yesterday $hour $yesterday $hour $lastsecond
done
the out put of the first code is like below : invalid number printf: 0 : invalid number printf: 0 T00:59:59Z), TO_DATE(2018-04-26
â Jhony
Apr 27 at 13:17
Like what below? Please EDIT YOUR QUESTION to show us what you actually want, rather than expecting us to read your mind or try and interpret poorly formatted comments.
â roaima
Apr 27 at 13:18
i have just modified printf to echo it is coming like this 0018-04-26sT%02d:00:00Z), TO_DATE(%sT%02d:59:59Z)n 2018-04-26
â Jhony
Apr 27 at 13:18
@Jhony don't useecho
where I've usedprintf
. They're different commands for a reason.
â roaima
Apr 27 at 13:18
i have created script file with the above code as test.sh and running in linux environment
â Jhony
Apr 27 at 13:19
 |Â
show 11 more comments
up vote
1
down vote
up vote
1
down vote
Try this.
#!/bin/bash
#
yesterday=$(date --utc --date 'yesterday' +'%Y-%m-%d')
for hour in 0..23
do
printf "https://abcd.com/logs?start=%sT%02d:00:00Z&end=%sT%02d:59:59Zn" $yesterday $hour $yesterday $hour
done
It won't handle the occasional leap seconds. If you need that, use this more complex code where the last range of the day may need to go from 23:00:00
to 23:59:60
, but ensure that your target application can also handle this:
#!/bin/bash
#
yesterday=$(date --utc --date 'yesterday' +'%Y-%m-%d')
leapsecond=$(date --utc --date @$(( $(date --utc --date '00:00:00' +%s) -1 )) +'%S')
lastsecond=59
for hour in 0..23
do
[[ hour == 23 ]] && lastsecond=$leapsecond
printf "https://abcd.com/logs?start=%sT%02d:00:00Z&end=%sT%02d:59:%02dZn" $yesterday $hour $yesterday $hour $lastsecond
done
Try this.
#!/bin/bash
#
yesterday=$(date --utc --date 'yesterday' +'%Y-%m-%d')
for hour in 0..23
do
printf "https://abcd.com/logs?start=%sT%02d:00:00Z&end=%sT%02d:59:59Zn" $yesterday $hour $yesterday $hour
done
It won't handle the occasional leap seconds. If you need that, use this more complex code where the last range of the day may need to go from 23:00:00
to 23:59:60
, but ensure that your target application can also handle this:
#!/bin/bash
#
yesterday=$(date --utc --date 'yesterday' +'%Y-%m-%d')
leapsecond=$(date --utc --date @$(( $(date --utc --date '00:00:00' +%s) -1 )) +'%S')
lastsecond=59
for hour in 0..23
do
[[ hour == 23 ]] && lastsecond=$leapsecond
printf "https://abcd.com/logs?start=%sT%02d:00:00Z&end=%sT%02d:59:%02dZn" $yesterday $hour $yesterday $hour $lastsecond
done
edited Apr 30 at 9:44
answered Apr 27 at 13:09
roaima
39.4k545106
39.4k545106
the out put of the first code is like below : invalid number printf: 0 : invalid number printf: 0 T00:59:59Z), TO_DATE(2018-04-26
â Jhony
Apr 27 at 13:17
Like what below? Please EDIT YOUR QUESTION to show us what you actually want, rather than expecting us to read your mind or try and interpret poorly formatted comments.
â roaima
Apr 27 at 13:18
i have just modified printf to echo it is coming like this 0018-04-26sT%02d:00:00Z), TO_DATE(%sT%02d:59:59Z)n 2018-04-26
â Jhony
Apr 27 at 13:18
@Jhony don't useecho
where I've usedprintf
. They're different commands for a reason.
â roaima
Apr 27 at 13:18
i have created script file with the above code as test.sh and running in linux environment
â Jhony
Apr 27 at 13:19
 |Â
show 11 more comments
the out put of the first code is like below : invalid number printf: 0 : invalid number printf: 0 T00:59:59Z), TO_DATE(2018-04-26
â Jhony
Apr 27 at 13:17
Like what below? Please EDIT YOUR QUESTION to show us what you actually want, rather than expecting us to read your mind or try and interpret poorly formatted comments.
â roaima
Apr 27 at 13:18
i have just modified printf to echo it is coming like this 0018-04-26sT%02d:00:00Z), TO_DATE(%sT%02d:59:59Z)n 2018-04-26
â Jhony
Apr 27 at 13:18
@Jhony don't useecho
where I've usedprintf
. They're different commands for a reason.
â roaima
Apr 27 at 13:18
i have created script file with the above code as test.sh and running in linux environment
â Jhony
Apr 27 at 13:19
the out put of the first code is like below : invalid number printf: 0 : invalid number printf: 0 T00:59:59Z), TO_DATE(2018-04-26
â Jhony
Apr 27 at 13:17
the out put of the first code is like below : invalid number printf: 0 : invalid number printf: 0 T00:59:59Z), TO_DATE(2018-04-26
â Jhony
Apr 27 at 13:17
Like what below? Please EDIT YOUR QUESTION to show us what you actually want, rather than expecting us to read your mind or try and interpret poorly formatted comments.
â roaima
Apr 27 at 13:18
Like what below? Please EDIT YOUR QUESTION to show us what you actually want, rather than expecting us to read your mind or try and interpret poorly formatted comments.
â roaima
Apr 27 at 13:18
i have just modified printf to echo it is coming like this 0018-04-26sT%02d:00:00Z), TO_DATE(%sT%02d:59:59Z)n 2018-04-26
â Jhony
Apr 27 at 13:18
i have just modified printf to echo it is coming like this 0018-04-26sT%02d:00:00Z), TO_DATE(%sT%02d:59:59Z)n 2018-04-26
â Jhony
Apr 27 at 13:18
@Jhony don't use
echo
where I've used printf
. They're different commands for a reason.â roaima
Apr 27 at 13:18
@Jhony don't use
echo
where I've used printf
. They're different commands for a reason.â roaima
Apr 27 at 13:18
i have created script file with the above code as test.sh and running in linux environment
â Jhony
Apr 27 at 13:19
i have created script file with the above code as test.sh and running in linux environment
â Jhony
Apr 27 at 13:19
 |Â
show 11 more comments
up vote
0
down vote
I am getting as expected output.
#!/bin/bash
for i in 00..23
do FROM_DATE=$(date -u -d "1 day ago" +%Y-%m-%d"T"$i":00:00Z")
echo "FROM_DATE:" $FROM_DATE | tr -d 'r'
TO_DATE=$(date -u +%Y-%m-%d"T"$i":59:59Z" -d "1 day ago")
echo "TO_DATE : $TO_DATE" | tr -d 'r'
done
Thanks a lot for your help
add a comment |Â
up vote
0
down vote
I am getting as expected output.
#!/bin/bash
for i in 00..23
do FROM_DATE=$(date -u -d "1 day ago" +%Y-%m-%d"T"$i":00:00Z")
echo "FROM_DATE:" $FROM_DATE | tr -d 'r'
TO_DATE=$(date -u +%Y-%m-%d"T"$i":59:59Z" -d "1 day ago")
echo "TO_DATE : $TO_DATE" | tr -d 'r'
done
Thanks a lot for your help
add a comment |Â
up vote
0
down vote
up vote
0
down vote
I am getting as expected output.
#!/bin/bash
for i in 00..23
do FROM_DATE=$(date -u -d "1 day ago" +%Y-%m-%d"T"$i":00:00Z")
echo "FROM_DATE:" $FROM_DATE | tr -d 'r'
TO_DATE=$(date -u +%Y-%m-%d"T"$i":59:59Z" -d "1 day ago")
echo "TO_DATE : $TO_DATE" | tr -d 'r'
done
Thanks a lot for your help
I am getting as expected output.
#!/bin/bash
for i in 00..23
do FROM_DATE=$(date -u -d "1 day ago" +%Y-%m-%d"T"$i":00:00Z")
echo "FROM_DATE:" $FROM_DATE | tr -d 'r'
TO_DATE=$(date -u +%Y-%m-%d"T"$i":59:59Z" -d "1 day ago")
echo "TO_DATE : $TO_DATE" | tr -d 'r'
done
Thanks a lot for your help
answered Apr 30 at 11:19
Jhony
144
144
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f440409%2fdate-time-loop-in-bash%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
it is considering the current time and doing the loop. Instead of that loop needs to start with 00:00:00 GMT time
â Jhony
Apr 27 at 12:12
Hard to read a script in a comment. I added it to the question, but please verify it. Something seems to have been eaten in the comment, so edit it to correct it.
â Philippos
Apr 27 at 12:44
that is correct. I took hour and appended the minutes and seconds.
â Jhony
Apr 27 at 13:09
Problem is my loop needs to start with GMT 00:00:00 instead of considering current hour. Please help it
â Jhony
Apr 27 at 13:10