Add mins in existing custom date format via shell - bash

I have problem that is using custom date format like date +%y%j.%H%M%S,
And what i want is to add 15 mins on the this date on just on current date of system. so that i can use for further calculation into my process.
I have tried with below code -
$uprBond=`date +%y%j.%H%M%S`
$ echo $uprBond
16079.031135
$ date -d "$(uprBond) + 5 minutes" +%y%j.%H%M%S
op > bash: uprBond: command not found
16079.035920
I am failing while passing the above date format , Can anybody please help on this.
Just for note, below is the piece of code is working when i used date function instead of defined date variable i.e. $uprBond (I don't want to use predefined date because we have some old same formatted date which needs that adding of mins).
date +%y%j.%H%M%S -d "`date` + 5 minutes";
op > 16079.040724

With GNU date, GNU bash 4 and its Parameter Expansion:
#!/bin/bash
uprBond="$(date +%y%j.%H%M%S)"
year="20${uprBond:0:2}"
doy="${uprBond#${uprBond:0:2}}"
doy="${doy%.*}"
time="${uprBond#*.}"
time="${time:0:2}:${time:2:2}:${time:4:2}"
in5min=$(date -d "${year}-01-01 +${doy} days -1 day +5 minutes ${time}" "+%y%j.%H%M%S")
echo "now: $uprBond"
echo "in 5min: $in5min"
Output:
now: 16079.145026
in 5min: 16079.145526

Related

Not able to get the expected date output after adding a second

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.

Create a set of files based off a date range

How would you go about doing a few lines of code in Bash to accomplish the following. I'm trying to build up my skills in Bash and learn how to handle more small tasks directory from the command line.
Steps:
Specify a start date and an end date. Load all the dates in between including the start and end date into a "list"
Loop over the list creating a file like this each time.
(requires date formatting)
2017-11-10.w
2017-11-11.w
2017-11-12.w
You could convert the input dates to Unix timestamps, then add the number of seconds per day and touch a file named after the result until you are past the end date:
#!/bin/bash
startstamp=$(date -d "$1" +'%s')
endstamp=$(date -d "$2" +'%s')
secs_per_day=$(( 24 * 3600 ))
for (( thedate = startstamp; thedate <= endstamp; thedate += secs_per_day )); do
touch "$(date -d "#$thedate" '+%F.w')"
done
The %s formatting string (a GNU extension) prints the number of seconds since the Unix epoch, and # in the argument to the -d option indicates that the date is in that format. %F is short for %Y-%m-%d, which translates to YYYY-MM-DD.
Example usage:
$ ./dates 2017-11-10 2017-11-15
$ ls -1
2017-11-10.w
2017-11-11.w
2017-11-12.w
2017-11-13.w
2017-11-14.w
2017-11-15.w
dates

Playing with dates in bash: passing variables

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"

Get next Sunday after a given date (bash)

Is there any built-in function to get next Sunday (next Monday, Tuesday, etc.) from a given date using bash shell script?
For example, what is the first Sunday after 1-Sep-2014? I expect 7-Sep-2014.
I have searched the answer in google, but only found this function :
date "+%Y%m%d" -d Sun
which is to get next Sunday after today.
I fear date can't do it directly. But you can use brace expansion to get the dates for all the following 7 days and select Sunday from them:
echo '1-Sep-2014\ +'{1..7}'\ days' | xargs -n1 date -d | grep Sun
You can use this little script:
#!/bin/bash
w=$(date -d"$1" +%w)
diff=$(( (7-$w)%7 ))
date -d"+$diff day $1" +%F
saved as sun.sh then:
kent$ ./sun.sh 01-Sep-2014
2014-09-07
kent$ ./sun.sh 31-Aug-2014
2014-08-31
kent$ ./sun.sh 3-Sep-2014
2014-09-07
kent$ ./sun.sh 23-Sep-2014
2014-09-28
Note that, argument validation was not done, I just showed how to calculate the date.
It is possible to make date do all the math without using shell code for math functions or to iterate though output looking for answers. The date function can do basic math like "x + 1 days" or "x - 1 month + 2 days".
It can also output formatted strings with arbitrary values.
The trick here is to use one date invocation to output the string used to calculate your final answer. We'll input your input date and ask for what day of the week that is, but at the same time output some math to subtract that number of days. By adding 7 days first we'll end up an the next week, then by subtracting the day-of-the-week number from that we'll have the next Sunday:
INPUTDATE="20140901"
date +"%Y%m%d" -d "$(date +"%Y%m%d + 7 days - %w days" -d "$INPUTDATE")"
This will spit out your expected answer: 20140907. Of course you can change the output format using the format string on the outer date invocation.

date calculations in bash

I have a simple question.
date '+%Y%m%d' --date='20130417 2 day ago'
20130415
works fine.
I have an env var
today="20130417"
but the following command does not work.
date '+%Y%m%d' --date='$today 2 day ago'
any workarounds?
You need double quotes instead of single:
$ date '+%Y%m%d' --date="$today 2 day ago"
20130415
Otherwise, the values within --date=' ' don't get evaluated.
This is a general behaviour, see an example:
$ echo 'the date is: $today'
the date is: $today
$ echo "the date is: $today"
the date is: 20130417

Resources