0

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?

intA
  • 2,513
  • 12
  • 41
  • 66
  • https://unix.stackexchange.com/q/49053/156990 –  Sep 18 '18 at 21:21
  • Showing the specific error you're having with prior approaches would be a good place to start. If the error message tells us you're trying to use commands meant for GNU `date` but you don't have that version, that gives us enough information to actually provide a useful answer. Right now, there's no specific problem here, so all we can do is guess. – Charles Duffy Sep 18 '18 at 21:37
  • *"...but it does not work"* is not a good problem statement. Please show the variable values and explain how it is not working. – jww Sep 18 '18 at 21:38

1 Answers1

1

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"
anubhava
  • 761,203
  • 64
  • 569
  • 643