How to read a date and create a variable with value following day/time - shell

Using GNU/Linux. Currently trying to read a date as input (yyyymmdd format) and create a variable which will hold value with next day date and a specific time - say 11am).
Any idea how to create it.
Tried with below, can it be altered for next day,
date —date=‘TZ=“EST” 09:00 next Fri’

read -p "Enter a date (yyyymmdd): " input_date
Calculate the next day's date :
next_day=$(date -d "$input_date + 1 day" +"%Y%m%d")
Combine the next day's date with the desired time
next_day_time="${next_day}110000"
Print the result to the console :
echo "Next day and time: $next_day_time"
Store the result in a variable :
next_day_var=$next_day_time
echo "Next day and time variable: $next_day_var"

Related

unix date util + bash get a "last monday" starting from a given date

I see that unix date util provides a way to create a date from a string with specified format.
And it can also create a date using such strings as "last monday" or X days ago.
Is it possible to get a last monday for any given date using just date util and bash.
Something akin
date -d "$(date -d '-24 day' +%Y%m%d) last monday"
This may not be your desired answer as it does not use the string such as last monday but how about:
given="Mar 16" # example
dow=$(date -d "$given" +%w) # day of week (0: Sun .. 6: Sat)
before=$(( (dow + 5) % 7 + 1 )) # days to go back
date -d "$given -$before days" +%Y%m%d # result

UNIX Script that calculates how many days and hours left until a person's birthday

I am trying to make a UNIX script that calculates how many days and hours are left until a birthday that the user inputs. I am only able to get the user to input his birthday and separate it into days month and year and the current date info but i think i'm doing the subtraction wrong
echo -n "Enter the birthdate (mm-dd-yyyy): "
read bdate
bmonth=${bdate:0:2}
bday=${bdate:3:2}
byear=${bdate:6:4}`
`cdate='date +%m-%d-%Y'/
cmonth=${cdate:0:2}
cday=${cdate:3:2}
cyear=${cdate:6:4}
diffdays=$(( ($bdate - $cdate) / (60*60*24) ))
echo $difdays
The below is working.
#!/bin/bash
## Just using a YYYY-MM-DD format
echo -n "Enter the birthdate (YYYY-MM-DD):"
read bdate
## convert to epoch time
bdate=`date -d "${bdate}" +"%s"`
## convert to epoch time
current_date=`date +"%s"`
## perform a calculation divide to find the no. of days
echo $(($((bdate - current_date)) / 86400))

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

Add mins in existing custom date format via shell

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

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.

Resources