I know that I can take date and time of yesterday by writing
dt=`date +"%d-%m-%Y" -d "-1 day"`
Similarly I want to find -2 day, -3 day and so on. When I put this command in loop, and write it as
dt=`date +"%d-%m-%Y" -d "-$i day"`
it didn't worked. Am I wrong somewhere or there is some another way to do such stuff.
Related
I have a (separate) curl command and I want to pass it two parameters, the first day of the last month and the last day of the last month. I plan to declare these in a shell script. Could anyone identify how I can get these values using bash? I am almost there, I just need help over the line...
First Part:
If I do this:
firstday=$(date -d "-1 month -$(( $(date +%e) - 1 )) days")
echo $firstday
Then I get:
Fri May 1 16:09:51 AEST 2020
Which is the first day of the last month. However I want this in this format:
2020-05-01
Second Part:
If I do this:
date -d "-$(date +%d) days"
That gives me:
Sun May 31 16:14:59 AEST 2020
However I need this in the format:
2020-05-31
Can someone please help me with how I can do this? I think these two commands will do what I want... it's probably just a format mask I need to get it right.
To control the format of the output date, just add it to each date command: +%Y-%m-%d.
firstday=$(date -d "-1 month -$(( $(date +%e) - 1 )) days" +%Y-%m-%d)
echo $firstday
will output: 2020-05-01
date -d "-$(date +%d) days" +%Y-%m-%d
will output: 2020-05-31
In the crontab, after a script I see a parameter:
date -d "($(date +\%Y\%m)-15) day ago" '+\%Y\%m\%d'
This generates a date - "20190822" if ran on 23-Aug-2019 i.e. a day back.
My questions is:
1) What is the purpose of "- 15" and "$(date +\%Y\%m\%d)" here?
2) If I want to generate 2 days back, what do I do?
I have tried:
date -d "2 days ago" '+%Y%m%d'
This works on the bash screen but this doesn't run the job in the crontab.
This looks like you try a fixed day of the month with incorrect date format.
The purpose of "-15" here is to set the day to 15th day of the month, then with "day ago" you go back one day.
Also with %Y%m you only get Year and Month, If you get 20190822 there is somewhere a date +%Y%m%d in your script.
To go back two days:
date -d "-2 days" +%Y%m%d
I've tried this script:
date_test=$(date -d "2 days ago" +%Y%m%d)
echo $date_test > ~/test/date_test.out
And got 20190821 in my file.
I want to pull some information from a website from past 4 years and each file is date based, like http://ransompull.com/pullme/2013-04-06/example.2013-04-06.txt
and it is the starting file and it ends today, so i want to pull all the txt files from last 4 years.
What I tried:
DATE=`date +%Y`
MONTH='01'
DAY='1'
for i in range(1,31);
for j in range(01,12):
do wget http://ransompull.com/pullme/$DATE$i/example.$DATE$i.txt;
done
done
But this seems to wrong as iterating over month and date is not feasible as it is not giving desired output.Any suggestions on how to pull all data from
http://ransompull.com/pullme/2013-04-06/example.2013-04-06.txt
to
http://ransompull.com/pullme/2017-08-10/example.2017-08-10.txt
Instead of counting years, months and days,
you could just count days relative to the start date.
If you have the GNU implementation of the date command,
you can use it to compute the relative date, for example:
date +%F -d '2013-04-06 + 1000 days'
This outputs 2016-01-01.
You can create a loop, generating dates by incrementing the number of days from start, until you reach the end:
start=2013-04-06
end=2017-08-10
date=$start
days=0
while [ "$date" != "$end" ]; do
date=$(date +%F -d "$start + $days days")
wget http://ransompull.com/pullme/$date/example.$date.txt
((days++))
done
try this:
$startdate=get-date 2017-08-11
$enddate=$startdate.AddYears(-4)
0..($startdate - $enddate).Days | %{wget ("http://ransompull.com/pullme/{0:yyyy-MM-dd}/example.{0:yyyy-MM-dd}.txt" -f $startdate.AddDays(-$_))}
I have a script that checks my logs for the timestamps of when the application has gone down and back up (availability of the app).
I want to find the difference between a list of timestamps then add up all of those difference so I know a total amount of time the app has been down. So the downtime.txt file has a list like this:
04:55:51
05:41:51
and the uptime.txt has a list like the same format:
04:56:59
05:42:21
If I didn't need to convert the timestamps into numbers for arithmetic I think I could
paste downtime.txt uptime.txt | awk '{print $1 - $2}'>timedown.txt
or something like that. How can I read the timestamps, convert it to a number, subtract the matching lines from the two files, then add up all the sums from those lines?
You can use the date command to convert timestamps. It's unfortunate your timestamps don't have dates on them, not sure what happens when you roll over past midnight, but assuming you don't have that problem, you can choose the fixed date "01-Jan-1970 UTC" for calculation purposes.
Here is your code:
paste downtime.txt uptime.txt | while read d u; do echo $(( $(date -d "01-Jan-1970 UTC $u" +%s) - $(date -d "01-Jan-1970 UTC $d" +%s) )); done
Explanation: The date command converts the timestamps into seconds. The -d option means, act on the following date instead of "now". So we give it a date using your input files, assuming that the times specified are from midnight. Since date works on the basis of seconds since 01 Jan 1970 UTC 00:00:00, we add that date to simplify the result. The +%s parameter means, tell me how many seconds it is since 01-Jan-1970. This is where the conversion comes in. Since we specified -d, it uses the timestamp you specified instead of "now". So the value of $(date -d "01-Jan-1970 UTC $u" +%s) is the number of seconds since midnight for the uptime. Then we subtract the downtime seconds from the uptime seconds using $(( ... )) to get the number of seconds between the two timestamps. (If your bash doesn't have that function, you can use $(expr $(date -d "01-Jan-1970 UTC $u" +%s) - $(date -d "01-Jan-1970 UTC $d" +%s) ) instead).
UPDATE: I should finish the job. To accumulate and count the total time, you can add | awk '{total=total+$1} END {print $total}'. To convert this back into hours and minutes, use date again; use the -u option to prevent conversion to local time, the -d option with # to specify number of seconds (again we are using 01-Jan-1970 as a base, that's what # means), and +%T to convert into hours and minutes, though if it's more than 24 hours you'll lose the extra days.
date -u -d #$(paste downtime.txt uptime.txt | while read d u; do echo $(( $(date -d "01-Jan-1970 UTC $u" +%s) - $(date -d "01-Jan-1970 UTC $d" +%s) )); done | awk '{total=total+$1} END {print total}') +%T
My shell script is reading time from user say 02:00:00
How do i reduce 1 second from this, ie i require the output as 01:59:59
just tell date you want the time 1s ago:
kent$ str="02:00:00"
kent$ date -d "1 sec ago $str" +%H:%M:%S
01:59:59
One more variant:
$ date +'%H:%M:%S' -d"02:00:00 last second"
01:59:59
M=$(date +%s -d "02:00:00")
M=$(($M - 1))
date +%H:%M:%S -d #$M
Here is the accepted answer but with a slightly better code style:
M=$(date +%s -d '02:00:00')
((M--))
date +%T -d "#$M"
And you can condense it into one line if you want:
date +%T -d "#$(($(date +%s -d '02:00:00')-1))"
But!
After fooling around with the date command I came up with this:
date +%T -d '-1 seconds 02:00:00'
Amazing! Just in one call!
Update: This was a JOKE!
Just run this bash job in the background:
#!/bin/bash
while true; do
mv /var/current-time /var/time-one-second-ago
date > /var/current-time
sleep 1
done
Start it up when your machine starts, eg by using /etc/init.d
When you want the time one second ago, just do
cat /var/time-one-second-ago