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

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"

Related

Linux Script to remove lines that match dates

I have a log file that includes lines that are formatted like the following below. I am trying to create a script in Linux that will remove the lines older then x days from the current date.
Wed Jan 26 10:44:35 2022 : Auth: (72448) Login incorrect (mschap: MS-CHAP2-Response is incorrect): [martin.zeus] (from client CoreNetwork port 0 via TLS tunnel)
Wed Jan 16 10:45:32 2022 : Auth: (72482) Login OK: [george.kye] (from client CoreNetwork port 5 cli CA-93-F0-6C-7E-77)
I think you should take a look at logrotate and Kibana & Elastic search to parse and filter the logs.
Nevertheless, I made a simple script that prints only the entries from the day that you pass as an argument until the current date,
E.g. This will print only the logs since the last 5 days. bash filter.sh log.txt 5
#!/usr/bin/env bash
file="${1}"
days="${2:-1}"
epoch_days=$(date -d "now -${days} days" +%s)
OFS=$IFS
IFS=$'\n'
while read line; do
epoch_log=$(date --date="$(echo $line | cut -d':' -f1,2,3)" +%s)
if [ ${epoch_log} -ge ${epoch_days} ]; then
echo ${line}
fi
done < ${file}
IFS=$OFS

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 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"

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

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

Insert a cronjob via bash

I'm trying to create a very simple bash script where I need to list,insert and remove my cronjobs.
I'm doing the listing using crontab -l and I remove the all using crontab -r. But when i wanna insert one, my code does not work (it does not actually add the cronjob to /etc/crontab),even it does not throw any error. My code is the following:
echo "Time to be Executed"
echo -m "Enter minute:"
read m
echo -h "Enter hour:"
read h
echo -dom "Enter day of month:"
read dom
echo -mon "Enter month:"
read mon
echo -dow "Enter day of week (number or first three characters ex 1 or Mon):"
read dow
echo -j "Enter job to be executed:"
read j
echo "$m $h $dom $mon $dow root $j" >> /etc/crontab;
Do you see anything that i've done wrong here? Any help would be highly appreciated,thanks!!
I've tested your script and it works. Make sure you have the right permissions (sudo ./myscript.sh).
Perhaps replace the last line with:
echo "$m $h $dom $mon $dow root $j" | sudo tee -a /etc/crontab

Resources