Print the path of a file a day before and a day after in Shell script - linux

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?

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

Related

how to check my file is created within 10 days in shell script

I have a bunch of log files which are named according to their creation dates. For example; if my log file is created on 12 March 2018, the name of the logfile is log-2018-03-12.log
Here is what I want to do: From today's date, I want to check the name of my log files and zip the log files which are created in last 10 days.
Here is my code that zip all log files in a specific directory:
#!/bin/bash
# current time
now=$(date +"%F")
backupfile="backup-$now"
scripthome=/opt/MyStore/action_scripts/deneme/
tendaysbefore= date -d "$now - 10 days" '+%F'
for file in $scripthome;
do
find "$(basename "$file")" | zip -R $backupfile.zip "log-2*.log"
done
But I want to zip last 10 days log file, not all log files, and also I want to continue doing it for every 10 days after this. Also, after having zip file, I want to delete old log files.
In other words, I am trying to write a log-backup script. Can you help me please?
Thank you very much!
#!/bin/bash
END=10
for ((i=1;i<=END;i++)); do
file=log-`date -d "$i days ago" +"%F"`.log
echo $file
done
With the above script you have file names for last 10 days. Later(inside loop) you can do whatever you want like adding it to existing zip or searching for its existence.
Edit:
Following code may be useful according to your requirement
#!/bin/bash
# current time
now=$(date +"%F")
backupfile="backup-$now"
scripthome=/home/bhanu/opt/MyStore/action_scripts/deneme/
tendaysbefore=`date -d "$now - 10 days" '+%F'`
for file in $scripthome;
do
zip -r -tt $now -t $tendaysbefore "$backupfile.zip" $scripthome/log-*.log > add.log 2>&1
zip "$backupfile.zip" -d "*" -tt $tendaysbefore > delete.log 2>&1
done

How do I get a date of yesterday in expect

I write a expect script 1.sh:
#!/usr/bin/expect
set yest [exec `date -d "yesterday" '+%Y%m%d'`]
send_user $yest
exit 1
And run in linux ,use expect -d ./1.sh
But I get this error:
expect version 5.44.1.15
executing commands from command file ./1.sh
invalid command name "/bin/date"
while executing
"date -d "yesterday" '+%Y%m%d'"
invoked from within
"set yest [date -d "yesterday" '+%Y%m%d']"
So,how do I fix this.I googled somethings and have no idea.
I find clock to instead date:
https://www.tcl.tk/man/tcl8.5/TclCmd/clock.htm
And I get a way to run expect in bash:
#!/bin/bash
expect <<!
## expect code ##
!
Finally use this way to get yesterday string in expect script:
set yest [clock scan "yesterday"]
set yest1 [clock format $yest -format {%Y%m%d}]
Try this
#!/usr/bin/expect
set yest [ exec /bin/date -d "yesterday" +%Y%m%d]
send_user $yest
exit 1
Try out this way:
For yesterday :
date -d '-1 day' '+%Y%d%m'
For day before yesterday :
date -d '-2 day' '+%Y%d%m'

How to create simple BASH Scripting, echo time and date

how to create a bash script
I want to echo good morning with time and date
#!/bin/bash
echo "Good Morning, today's date is $( date )"
echo "Quote of the Day"
echo "Today I will try to be less lazy than I was yesterday"

How to read date from user in linux and use that date in svn log

I am new to shell scripting.
I want to write a shell script to get the date from user from the terminal in format yyyy-mm-dd and use that date as start date to get the revision changes made in svn repository for a particular date range.
My script is:
echo "Date"
echo "Enter year"
read Y
echo "Enter month"
read M
echo "Enter Date"
read D
D1=`expr $D + 3`
svn log -r {$Y-$M-$D}:{$Y-$M-$D1} http://svn.abc.com/svn/trunk.
This is the script I have written.
I know I should use date function not sperately Y M D.
And also I want to list only revision numbers not full log messages as svn log command shows.
I would use the date utility for verification:
while true; do
read -p "Enter a date (YYYY-mm-dd): " user_date
if date=$(date -d "$user_date" +%F); then
# user date was ok
break
fi
done
date3=$(date -d "$date + 3 days" +%F)
svn log -r "$date:$date3" ...
You really need to use date, especially to add 3 days: you don't want to end up with "2014-02-30"

Compare last modification dates of local and remove files

I can get a last modification date of the remote file using
curl --head http://url 2>/dev/null | grep -Po '(?<=^Last-Modified:\s).*$'
This gets me date/time such as
Wed, 04 Sep 2013 19:53:18 GMT
For local file I can use
find /path/file -exec stat \{} --printf="%y\n" \;
and it gets me date/time such as
2012-01-09 09:50:30.000000000 -0500
How can I compare this dat/time with last modification date of the local file? Please note that time zone may be different for remote and local file.
You can actually use date -d to parse the string, as #fedorqui says. Try running the below commands:
$ date -d "$(<your curl command grepped>)" +%s #+%s gets you timestamp.
$ date -d "$(<your find command>)" +%s
To actually compare, you can subtract the timestamps, something like:
$ echo $(( $(date -d "$(<curl cmd>)" +%s) - $(date -d "$(<find cmd>)" +%s) ))

Resources