2

I am having a shell script like below

#!/bin/bash

TIMESTAMP=`date "+%Y-%m-%d"`
path=/home/$USER/logging/${TIMESTAMP}/status/${TIMESTAMP}.fail_log

echo filePath=$path

In this script I want to print the path of the failed logs for that particular timestamp.

Now I am able to get the echo to print the path.

How do I print a day before and day after the timestamp? Is it possible to do that?

How Can I do that in a single line of code? Can we do that?

  • Have you actually searched, using the related [SO questions](http://stackoverflow.com/questions/15374752/get-yesterdays-date-in-bash-on-linux-dst-safe)? – t0mm13b May 08 '17 at 17:46
  • `date --date="yesterday"`, `date --date="tomorrow"` – gaganshera May 08 '17 at 17:48

1 Answers1

1

To get tomorrow's data, you can do:

date -d '+1 day' "+%Y-%m-%d"

To get yesterday's data, you can do:

date -d '-1 day' "+%Y-%m-%d"

To use it in script:

#!/bin/bash

nextDate=$(date -d '+1 day' "+%Y-%m-%d")
prevDate=$(date -d '-1 day' "+%Y-%m-%d")

nextDatePath=/home/$USER/logging/${TIMESTAMP}/status/${nextDate}.fail_log

prevDatePath=/home/$USER/logging/${TIMESTAMP}/status/${prevDate}.fail_log
anubhava
  • 761,203
  • 64
  • 569
  • 643