Bash: How to pass a variable to a command - bash

I need to substract 10 minutes from a given time.
As I understand from previous answers, there are solutions only for given constant input, for example:
date --date '20140202 11:45:30 UTC -10 min'
But in my script, a given date is read from file and I have it as argument, for example:
myDate="20140202 11:45:30"
How can I pass this argument (myDate)to the command above?

myDate="20140202 11:45:30"
date --date "${myDate} UTC -10 min"

Related

remove millisecond from echo time bash

1633036680022 , This is epoch result i got from elasticsearch.
if i tried to convert this epcho to human-readable date,
So i used epochconverter
And i used bash command to convert this in my terminal,
$ date -d #1633036680022
Tuesday 15 November 53718 05:30:22 PM IST
This output from terminal say the Year 53718, because the epoch '1633036680022' is in milliseconds.
All i want is ,epoch in seconds.
You can divide by 1000 and convert to timestamp.
date -d #"$(echo "1633036680022/1000" | bc)"
Strip milliseconds with bash (output only first 10 digits):
x="1633036680022"
date -d "#${x:0:10}"

Command to get milliseconds by static time given

I found command like this $(date +%s%3N) to convert current time to milliseconds, but example I have time like this 2018-25-08 12:00 PM. How to convert it to millisecond in bash?
Finally, I found this command to convert static date to milliseconds
date -d 'date -d '08/23/2018 23:59:59' +"%s%3N"

Yesterday's date variable in BASH on AIX Server

I want to store yesterday's date in BASH variable to search for yesterday's files with that variable in the file-name wildcard search. I am using the following format for New York City, NY, USA (EST) time zone, and wanted to know whether it is guaranteed to fetch yesterday's date from the system date; else I can make further changes to the variable.
yesterday=$(TZ=GMT+28 date +%Y%m%d)
...
for file in $HOME_DIR/*$yesterday*.txt;
...
The text filename in HOME_DIR would be as follows:
"ABC_20171011064612.txt"
update 1: Attempt for removing daylight savings related issues:
yesterday=$(echo -e "$(TZ=GMT+28 date +%Y%m%d)\n$(TZ=GMT+18 date +%Y%m%d)"|grep -v $(date +%Y%m%d)|sort|tail -1)
1) Convert two dates to string, 24 hours and 14 (picked arbitrarily to be less than 24 hours) hours before today's date
2) Filter for dates that are not today's date
3) Sort strings from 2) in ascending order
4) Assign yesterday variable to last tail -1 entry of the list
It may not be always right due to DST, although it will not be a big issue.
You could rather say:
yesterday=$(date -d yesterday +%Y%m%d)
You attempted
yesterday=$(echo -e "$(TZ=GMT+28 date +%Y%m%d)\n$(TZ=GMT+18 date +%Y%m%d)|
grep -v $(date +%Y%m%d)|sort|tail -1)
I think it worked.

Construct a new date by adding X number of hours (Unix Shell)

Given a UNIX date variable, I need to construct a new date adding a certain number of hours (must be variable) to the initial date. I've looked at this post, which explains how to do this with the current date. For example with 1 hour, date -d '+1 hour' '+%F %T' returns the time it will be in exactly one hour. However, I need to do this with a date variable, not with the current date.
I've tried messing around with the -d flag, but if I set the date to another date variable, I can't figure out how to change it again (such as adding another X number of hours).
Is there a good way to do this? Am I on the right track with the -d flag or is there a better way?
Thanks!
You can use:
# your date variable
dt=$(date -d '2016-08-15 11:10:15')
# add 1 hour to $dt now
date -d "$dt +1 hour"
Mon Aug 15 12:10:15 EDT 2016

UNIX Shell scripting (digit based subtraction)?

I have an input like 2013_07_02 (It may be any date). I want to retrieve date which is 2 days back from the input date. How to do so?
In case you want the output in the same format as the input, i.e. 2013-07-02, substitute _ with - before passing to date:
$ inputdate='2013_07_02';
$ date --date=${mydate//_/-}'-2 day' +'%Y_%m_%d'
2013_06_30
date is your friend:
date -d "2013-01-01 -2 days" +"%Y-%m-%d"
This prints out 2012-12-30.
use date with -d option. It understands relative dates, even with common language like
date -d "two days ago".
In your case something like
date -d "2013-07-01 -2 days"
would be enough.

Resources