I am trying to use bash to produce a list of dates and times between a starting point and an end point.
I would like the output to be in mm/dd/yyyy hh:mm format.
On the command line, the command:
date +"%m/%d/%Y %H:%M"
Produces the output that I am looking for.
When I use the line that is presently commented out, I get an error.
date: extra operand ‘%H:%M’
Try 'date --help' for more information.
I am not sure how to alter the script to produce the output that I am looking for.
DATE=date
#FORMAT="%m/%d/%Y %H:%M"
FORMAT="%m/%d/%Y"
start=`$DATE +$FORMAT -d "2013-05-06"`
end=`$DATE +$FORMAT -d "2013-09-16"`
now=$start
while [[ "$now" < "$end" ]] ; do
now=`$DATE +$FORMAT -d "$now + 1 day"`
echo "$now"
done
I have played around with adding an 00:00 after the start and end times, but that did not work.
Any ideas where I am getting the output format wrong?
Code from:
https://ocroquette.wordpress.com/2013/04/21/how-to-generate-a-list-of-dates-from-the-shell-bash/
When you use the FORMAT="%m/%d/%Y %H:%M" you need quotes because it contains a space, so:
now=`$DATE +"$FORMAT" -d "$now + 1 day"`
Also, I do not think that you can compare dates like that. You might need timestamp:
date +%s
Related
What I'm trying to do below
echo -e "input start_time(YYYYMMDDHHMMSS)"
read word1
dt= `date "--date=${word1} -s + 1sec" +%Y%m%d%H%M%S`
echo ${dt}
If input : 20181201090909 then I got an output : 20181201090909030001. The expected output is quite incorrect. I was expecting it to be 20181201090910
You need to format differently the string you pass to date:
~$ date +%Y%m%d%H%M%S --date="20181201 09:09:09 + 1sec"
20181201090910
~$
You see a space between the date and the time, and the colon separating hour minutes seconds. See info date for more information.
I have a string from conf file (lets call for example date1):
#!/bin/bash
# it is example
date1="201605250925"
datenow="$(date +%Y%m%d%H%M -d "+1hour")"
date2=$(date +%Y%m%d%H%M -d "$date1 + 1hour")
# NOT WORK?
echo "$date1 --> $date2"
# WORK!
echo "$date1 --> $datenow"
I need to add 1 hour. But getting error like this:
date: invalid date `201605250925 + 1hour'
But its work for datenow.
How can I user addhour for custom date format from string?
You need a format that meets the command date expectations, something like:
2016-05-25 09:25
The space denote the start of time and the time format is HH:MM.
That comes from then international ISO 8601, but using an space instead of a T.
If the format is fixed, we can use bash internal capacities (no external command except date used) to change it like this:
#!/bin/bash
d1="201605250925"
dc="${d1:0:8} ${d1:8:2}:${d1:10:2}+0"
d2=$(date +'%Y%m%d %H:%M' -ud "$dc + 1 hour" )
echo "$d2"
Or POSIXly (dash) with no call needed to sed, awk or cut (faster):
#!/bin/dash
d1="201605250925"
dt=${d1##????????}
dc="${d1%%"$dt"} ${dt%%??}:${dt##??}+0"
d2=$(date -ud "$dc + 1 hour" +'%Y%m%d %H:%M')
echo "$d2"
20160525 10:25
The inclusion of a +0 after the time in dc: 20160525 09:25+0
will ensure that date will interpret the time as with offset 0 (UTC).
The use of the option -u to date will ensure that the value read in UTC also change in UTC, avoiding any Daylight correction or local time change.
If you want to keep the same format as your input string you could use cut or sed to split it:
cut
d1="201605250925"
ds=$(echo $d1 | cut --output-delimiter=" " -c 1-8,9-12); \
d2=$(date '+%Y%m%d%H%M' -ud "$ds + 1hour")
sed
d1="201605250925"
ds=$(echo $d1 | sed 's/\(.*\)\(....\)$/\1 \2/g'); \
d2=$(date '+%Y%m%d%H%M' -ud "$ds + 1hour")
This way the date utility can make sense of the date and time.
Result:
$ echo $d2
201605251025
I'm trying to make some calculations with date in bash script but can't find out the right syntax. I get a string from a file that I convert to a date. Then I want to get the date for one and two days ahead. Looking on another StackOverflow posts it looked easy adding days to today date. This is what I am doing now:
# Extract date string from file
ctldate=`awk 'NR==8 { print $4 }' a-AC-2015-02-10-120000-g3.ctl`
echo $ctldate
12:00Z10feb2015
# Convert string to date
ctldate2=`date +'%d/%m/%Y' -d $ctldate`
echo $ctldate2
10/02/2015
# Try to add a day, should be 11/02
data1=$(date +'%d/%m/%Y' -d "$ctldate" --date='1 day')
echo $data1
12/02/2015
# Also tried
data1=$(date +'%d/%m/%Y' -d "$ctldate2" --date='1 day')
echo $data1
12/02/2015
# And
data1=`date +'%d/%m/%Y' -d $ctldate --date='1 day'`
echo $data1
12/02/2015
It seems that I'm not properly passing $ctldate var to command and that the base date for calculation is today.
Thanks in advance
When you pass several -d or --date, date uses the last one. So when you do:
date -d "$ctldate" --date='1 day'
date will take into account --date='1 day' as it's the last -d/--date argument, and will happily show you tomorrow's date.
Instead you should use:
date -d "$ctldate +1 day"
I want to add the code below to my script but it's not showing the total_time value although CurrentTime is showing correctly. In this I want to change epoch time to current system time and then add 20 minutes to it.
CurrentTime=`date -d #$2`
echo "CurrentTime : $CurrentTime " >> ${LOGFILE}
Total_time=`"$CurrentTime" -d "+20 min"`
How can I do it?
Change your totaltime assignment like this:
Total_time=`date -d "$CurrentTime +20 mins"`
The reason this isn't working is that by the time you're trying to assign the value to $Total_time, your $CurrentTime variable has already been set to a time. It's not a command anymore, it's a string that is the result of a command.
Each time you want to calculate a new date, you need a new invocation of the `date` command. That's what Guru's answer provides you with, though he didn't explain why.
If what you need is to make a "base" date to which you apply modifiers, you can still do this, but I'd recommend a slightly different notation:
#!/bin/bash
start=$(date '+%s')
# do stuff
sleep 20
duration=$((`date '+%s'` - $start))
You can then use your $duration as an easier basis for other calculations, AND you can use it with relative dates:
printf "[%s] Start of job\n" "$(date -d #$start '+%Y-%m-%d %T')"
...
printf "[%s] End of job\n" "$(date -d #"$((start + duration))" '+%Y-%m-%d %T')"
Probably better to format your log files in a more standard format than date's default.
I need to create a bash shell script starting with a day and then loop through each subsequent day formatting that output as %Y_%m_d
I figure I can submit a start day and then another param for the number of days.
My issue/question is how to set a DATE (that is not now) and then add a day.
so my input would be 2010_04_01 6
my output would be
2010_04_01
2010_04_02
2010_04_03
2010_04_04
2010_04_05
2010_04_06
[radical#home ~]$ cat a.sh
#!/bin/bash
START=`echo $1 | tr -d _`;
for (( c=0; c<$2; c++ ))
do
echo -n "`date --date="$START +$c day" +%Y_%m_%d` ";
done
Now if you call this script with your params it will return what you wanted:
[radical#home ~]$ ./a.sh 2010_04_01 6
2010_04_01 2010_04_02 2010_04_03 2010_04_04 2010_04_05 2010_04_06
Very basic bash script should be able to do this:
#!/bin/bash
start_date=20100501
num_days=5
for i in `seq 1 $num_days`
do
date=`date +%Y/%m/%d -d "${start_date}-${i} days"`
echo $date # Use this however you want!
done
Output:
2010/04/30
2010/04/29
2010/04/28
2010/04/27
2010/04/26
Note: NONE of the solutions here will work with OS X. You would need, for example, something like this:
date -v-1d +%Y%m%d
That would print out yesterday for you. Or with underscores of course:
date -v-1d +%Y_%m_%d
So taking that into account, you should be able to adjust some of the loops in these examples with this command instead. -v option will easily allow you to add or subtract days, minutes, seconds, years, months, etc. -v+24d would add 24 days. and so on.
#!/bin/bash
inputdate="${1//_/-}" # change underscores into dashes
for ((i=0; i<$2; i++))
do
date -d "$inputdate + $i day" "+%Y_%m_%d"
done
Very basic bash script should be able to do this.
Script:
#!/bin/bash
start_date=20100501
num_days=5
for i in seq 1 $num_days
do
date=date +%Y/%m/%d -d "${start_date}-${i} days"
echo $date # Use this however you want!
done
Output:
2010/04/30
2010/04/29
2010/04/28
2010/04/27
2010/04/26
You can also use cal, for example
YYYY=2014; MM=02; for d in $(cal $MM $YYYY | grep "^ *[0-9]"); do DD=$(printf "%02d" $d); echo $YYYY$MM$DD; done
(originally posted here on my commandlinefu account)
You can pass a date via command line option -d to GNU date handling multiple input formats:
http://www.gnu.org/software/coreutils/manual/coreutils.html#Date-input-formats
Pass starting date as command line argument or use current date:
underscore_date=${1:-$(date +%y_%m_%d)}
date=${underscore_date//_/-}
for days in $(seq 0 6);do
date -d "$date + $days days" +%Y_%m_%d;
done
you can use gawk
#!/bin/bash
DATE=$1
num=$2
awk -vd="$DATE" -vn="$num" 'BEGIN{
m=split(d,D,"_")
t=mktime(D[1]" "D[2]" "D[3]" 00 00 00")
print d
for(i=1;i<=n;i++){
t+=86400
print strftime("%Y_%m_%d",t)
}
}'
output
$ ./shell.sh 2010_04_01 6
2010_04_01
2010_04_02
2010_04_03
2010_04_04
2010_04_05
2010_04_06
2010_04_07