How do I add time to a date in unix shell script? [duplicate] - linux

This question already has answers here:
Date arithmetic in Unix shell scripts
(14 answers)
How do I do date math in a bash script on OS X Leopard?
(8 answers)
Calculating time (adding minutes) bash
(1 answer)
Subtract days from a date in Bash
(7 answers)
Closed 4 years ago.
I have a file that contains date values. I want to be able to pull the last line of the file, formatted like "'2018-09-18 16:42:57'" add 1 day to it and store that into a variable. The code I have right now looks like the following, but it does not work:
start_date=$(tail -n 1 run_dates.txt)
start_date=$(start_date -d "+1 day")
What is the correct syntax to do this?

You can use this one-liner gnu date command to extract last line of the file, add one day and store output in a variable:
start_date=$(date -d "$(tail -n 1 run_dates.txt) +1 day" '+%Y-%m-%d %T')
To check variable content use:
declare -p start_date
declare -- s="2018-09-19 11:42:57"

Related

ccyymmddhhmmss format for shell script [duplicate]

This question already has an answer here:
How to convert any date to YYYYMMDDHHMMSS using unix shell script?
(1 answer)
Closed 1 year ago.
How to create text files with date and time as per below format:
vendor_brandname_ccyymmddhhmmss.txt
currently I am using below format:
DATE=date +%Y%m%d
file1=HospTOHome_BRC_$DATE.txt
The + argument isn't limited to just placeholders.
$ date +"HsopTOHome_BRC_%Y%m%d%H%M%S.txt"
HsopTOHome_BRC_20210630082847.txt
You can set the value of file1 with a command substitution:
$ file1=$(date ...)
To actually create the file, you can use touch:
$ touch "$file1"
or redirection the output of some command that generates the contents you want:
$ ls > "$file"
or pass it as the argument to your favorite text editor:
$ vi "$file"

Output exactly x number of characters to variable using read in Bash [duplicate]

This question already has answers here:
Extract substring in Bash
(26 answers)
Closed 6 years ago.
How to retrieve the first 10 characters of a variable with Bash?
FOO="qwertzuiopasdfghjklyxcvbnm"
I need to get qwertzuiop.
If the variable is: FOO="qwertzuiopasdfghjklyxcvbnm"
then
echo ${FOO:0:10}
will give the first 10 characters.
Use the head command.
echo $FOO | head -c 10
=> qwertzuiop

Why set variable with output of bash command use only first line? [duplicate]

This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
When to wrap quotes around a shell variable?
(5 answers)
Closed 2 years ago.
I want to use:
schema=$(kubectl exec -n $namespace -it $podName -- bash -c "./spiral orm:schema")
echo $schema
But eventually in schema variable recorded only the first line from the result of bash execution.
How to make it use all lines?

Shell: insert variable into a command [duplicate]

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 4 years ago.
I'm trying to find a way the use a variable in this command to replace -10 with n_days var:
n_days= -10
date_prefix=$(date -d '-10 day' +%Y/%m/%d)
I tried this way but it didn't work:
date_prefix=$(date -d '${n_days} day' +%Y/%m/%d)
Two things:
Declare your variable properly (there is a space in your example)
Use double quotes in place of single quotes to allow the variable to be interpolated
So:
n_days=-10
date_prefix=$(date -d "$n_days day" +%Y/%m/%d)

Grep for last X hours [duplicate]

This question already has answers here:
Extract data from log file in specified range of time [duplicate]
(5 answers)
Closed 7 years ago.
I have a log file. Each line is prepended with a date and the date format is as below:
2016-02-01 11:34:48,567.......
How do I grep this file for last 24 hours?
I tried few things such as below for specific time but this approach wont work for last X hours:
grep "2016-02-01 15:0[1-9]:00 logfile
Also, the following does work for -1hour but fails for -23 or -24hours:
grep "^$(date -d -23hour +'%Y-%m-%d %H')" logfile
You can use sed if lines are arranged in chronological order.
sed -e "1,/^$(date -d -23hour +'%Y-%m-%d %H')/d"
This will delete all lines until the first match is found ie print all line after the first match till the end of file.

Resources