bash date environment variable - bash

I need to calculate a date using a date string, derived from command line variables in a bash script.
Assume I have a set of command line arguments (CLA's) such as $1=2013 $2=03 $3=22 and I want to get the formatted date string for a date before, or afterward by some number of days.
I can always do, without the CLAs,
date -d "20130322 -1 day" -u +%Y%m%d
but when I try something like,
yyyy=2013
month=03
day=22
yest='"'$yyyy$month$day'" -1 day"'
yesterday=$(date -d $yest -u +%Y%m%d)
I get "date: invalid option -- '1'"
Where have I gone wrong?

Apparently you overcombined with quotes
pwadas#host:~$ echo $(date -d "20130322 -1 day" -u +%Y%m%d)
20130321
pwadas#host:~$ xx=20130322
pwadas#host:~$ echo $(date -d "$xx -1 day" -u +%Y%m%d)
20130321
pwadas#host:~$
pwadas#host:~$ yesterday=$(date -d "$xx -1 day" -u +%Y%m%d)
pwadas#host:~$ echo $yesterday
20130321
pwadas#host:~$
PS. above does not work with bash on MacOSX, but on Linux does..

Related

bash - command in a variable - issue with quotes

I want to store the date of last Sunday in a variable.
#/bin/bash
OFFSET=$(date +%u)
COMMAND="date --date='"$((OFFSET))" days ago' +%Y%m%d"
DATEFULL=$($COMMAND)
echo offset $OFFSET
echo command $COMMAND
echo datefull $DATEFULL
Output:
[root#localhost ~]# ./test.sh
date: extra operand ‘ago'’
Try 'date --help' for more information.
offset 1
command date --date='1 days ago' +%Y%m%d
datefull
Pasting the result of $COMMAND in command line gives the right result:
[root#localhost ~]# date --date='1 days ago' +%Y%m%d
20210822
So why does he claim about "date: extra operand ‘ago'’" ? How can I execute the result of command and set it in a variable?
Try to use arrays for commands, instead of variables :
#!/bin/bash
OFFSET=$(date +%u)
COMMAND=(date "--date=$OFFSET days ago" +%Y%m%d)
DATEFULL=$("${COMMAND[#]}")
echo offset $OFFSET
echo command $COMMAND
echo datefull $DATEFULL
Also all uppercase variable names can cause clashes with bash-defined variables.
I'm unsure why this doesn't work. I think it has something to do with bash parsing quotes out of the command. If you use eval, it works:
#/bin/bash
OFFSET=$(date +%u)
COMMAND="date --date='"$((OFFSET))" days ago' +%Y%m%d"
DATEFULL=$(eval "$COMMAND")
echo offset $OFFSET
echo command $COMMAND
echo datefull $DATEFULL
Output
$ OFFSET=$(date +%u)
$ COMMAND="date --date='"$((OFFSET))" days ago' +%Y%m%d"
$ DATEFULL=$(eval "$COMMAND")
$ echo offset $OFFSET
offset 1
$ echo command $COMMAND
command date --date='1 days ago' +%Y%m%d
$ echo datefull $DATEFULL
datefull 20210822

What are shell command to get current date and date 3 hrs back?

To get the current command we simple shell script command
date
if you want to get a custom date format
NOW=$(date +"%Y-%m-%d %T")
Am not getting simple command to get the 3hrs back date with above format , any one can help me please .
i used below shell command with perl like below , but i do not want another additional dependency perl , i just need to shell command
CURRENT_DATE=`perl -e 'use POSIX qw(strftime);$d = strftime "%Y-%m-%d %H:%M:%S", localtime(time());print $d'`
CURRENT_DATE_MINUS_3_HRS_DATE=`perl -e 'use POSIX qw(strftime);$d = strftime "%Y-%m-%d %H:%M:%S", localtime(time() - 10800);print $d'`
The date command has the nice keyword ago for the date you can give with -d option:
date -d '3 hours ago' +"%Y-%m-%d %T"
Shows the current date:
date "+%Y-%m-%d %T"
Show date back to 3 hours:
date -d "-3 Hours" "+%Y-%m-%d %T"
Some of the other things you can try:
# Back to 3 days
date -d "-3 Days" "+%Y-%m-%d %T"
# Next to 3 days
date -d "3 Days" "+%Y-%m-%d %T"
# Back to 2 Years and 3 Hours
date -d "-2 Years -3 Hours" "+%Y-%m-%d %T"
Using the BSD date that ships with macOS, use the -v option.
$ date +"%Y-%m-%d %T"
2018-11-08 16:25:21
$ date -v -3H +"%Y-%m-%d %T"
2018-11-08 13:25:26

Using a Loop To Search Only Logs In A Time Window

I'm trying to find a pattern "INFO: Server startup in" for last 5 mins in a log file.
Here is the line from which I'm trying to find the pattern: "INFO | jvm 1 | main | 2018/07/09 00:11:29.077 | INFO: Server startup in 221008 ms"
The pattern is coming, but I need to shorten the code or create a loop for it.
I tried to create a loop, but it is not working. Here is my code without loops, which is working:
#!/bin/bash
#Written by Ashutosh
#We will declare variables with date and time of last 5 mins.
touch /tmp/a.txt;
ldt=$(date +"%Y%m%d");
cdt=$(date +"%Y/%m/%d %H:%M");
odtm5=$(date +"%Y/%m/%d %H:%M" --date "-5 min");
odtm4=$(date +"%Y/%m/%d %H:%M" --date "-4 min");
odtm3=$(date +"%Y/%m/%d %H:%M" --date "-3 min");
odtm2=$(date +"%Y/%m/%d %H:%M" --date "-2 min");
odtm1=$(date +"%Y/%m/%d %H:%M" --date "-1 min");
## Finding the pattern and storing it in a file
grep -e "$odtm1" -e "$cdt" -e "$odtm2" -e "$odtm3" -e "$odtm4" -e
"$odtm5" /some/log/path/console-$ldt.log
> /tmp/a.txt;
out=$(grep 'INFO: Server startup in' /tmp/a.txt);
echo "$out"
## remove the file that contains the pattern
rm /tmp/a.txt;
I have tried to use sed also, but date function is not working with it.
Can someone please give me the new changed script with loops?
Adopting your original logic:
time_re='('
for ((count=5; count>0; count--)); do
time_re+="$(date +'%Y/%m/%d %H:%M' --date "-$count min")|"
done
time_re+="$(date +'%Y/%m/%d %H:%M'))"
ldt=$(date +'%Y%m%d')
awk -v time_re="$time_re" '
$0 ~ time_re && /INFO: Server startup in/ { print $0 }
' "/some/log/path/console-$ldt.log"
Performance enhancements are certainly possible -- this could be made much faster by bisecting the log for the start time -- but the above addresses the explicit question (about using a loop to generate the time window). Note that it will get unwieldy -- you wouldn't want to use this to search for the last day, for example, as the regex would become utterly unreasonable.
Sounds like all you need is:
awk -v start="$(date +'%Y/%m/%d %H:%M' --date '-5 min')" -F'[[:space:]]*[|][[:space:]]*' '
($4>=start) && /INFO: Server startup in/
' file
No explicit loops or multiple calls to date required.
Here is a bash script that does the job (thanks to Charles for its improvement):
#!/bin/bash
limit=$(date -d '5 minutes ago' +%s)
today_logs="/some/log/path/console-$(date +'%Y%m%d').log"
yesterday_logs="/some/log/path/console-$(date +'%Y%m%d' -d yesterday).log"
tac "$today_logs" "$yesterday_logs" \
| while IFS='|' read -r prio jvm app date log; do
[ $(date -d "$date" +%s) -lt "$limit" ] && break
echo "|$jvm|$prio|$app|$date|$log"
done \
| grep -F 'INFO: Server startup in' \
| tac
It has the following advantages over your original script:
optimized: it parses log lines starting from the more recent ones and stops at the first line encountered that is more than 5 min old. At 23:59, no need to parse log lines from 0:00 to 23:53
arbitrary time window: you can replace "5 minutes" with "18 hours" and it will still work. A time window of more than one day needs adaptation since each day has it own log file
works correctly when day changes: at 0:00 the original script will never parse the log lines from 23:55:00 to 23:59:59
Mixing the above code with Ed Morton's answer, you get:
#!/bin/bash
limit=$(date -d '5 minutes ago' +'%Y/%m/%d %H:%M')
today_logs="/some/log/path/console-$(date +'%Y%m%d').log"
yesterday_logs="/some/log/path/console-$(date +'%Y%m%d' -d yesterday).log"
tac "$today_logs" "$yesterday_logs" \
| awk -v stop="$limit" -F'[[:space:]]*[|][[:space:]]*' '
($4 < stop) { exit }
/INFO: Server startup in/
' \
| tac

Shell Script for Yesterdays Date

I have tried adding -d "yesterday" but I haven't had any luck getting it to work. Here is what I have for the whole script:
#! /bin/bash
saveDir="TJ"
dd=$(date +"%m-%d-%Y")
for file in *.csv ; do
saveName="${saveDir}/TJ ${dd}.csv"
cut -d',' -f2,14 "$file" > "$saveName"
done
how do I get dd to output yesterdays date instead of the current date?
EDIT: This is what I have now
#! /bin/bash
saveDir="TJ"
dd=$(date --date='yesterday' +'%m-%d-%Y')
for file in *.csv ; do
saveName="${saveDir}/TJ ${dd}.csv"
cut -d',' -f2,14 "$file" > "$saveName"
done
the above is saving the file as TJ .csv but I'm not sure what was done incorrectly
I think you want to use -
$ cat test.sh
#!/bin/bash
dd=$(date --date='yesterday' +'%m-%d-%Y')
echo $dd
$ ./test.sh
12-31-2013
or you could use
$ date -d '1 day ago' +'%m-%d-%Y'
12/31/2013
And for tomorrow -
$ date -d '1 day' +'%m-%d-%Y'
01/02/2014
or
$ date --date='tomorrow'
Thu Jan 2 21:25:00 EST 2014
Get today's date in seconds since epoch. Subtract 86400 to get to yesterday. Then convert yesterday to the string format you want.
today=`date +"%s"`
yesterday=`expr $today - 86400`
dd=`date --date="#${yesterday}" +"%m-%d-%Y"`
Try this
yday=$(date --date yesterday "+%d-%m-%Y")
echo $yday
And If you works in Linux
yday=$(date -d "-1 days" +"%d-%m-%Y")
echo $yday
I tried date -d "yesterday" +%m-%d-%Y on my Linux, it worked fine.
If you are on unix platform, you cannot use -d,
you can get yesterday's date using perl, this is how I do using perl
dd=$(perl -e '($a,$b,$c,$day,$mon,$year,$d,$e,$f) = localtime(time-86400);printf "%02d-%02d-%4d",$day, $mon+1, $year+1900')
echo $dd
01-01-2014
NewDate=`date +"%A %d %B %Y" --date="-1 day"`
echo $NewDate
this will give your yesterday's date (-1)
This will give you tomorrow's date (+1)
even you can check for any values like (+/-) days

YYYY-MM-DD format date in shell script

I tried using $(date) in my bash shell script, however, I want the date in YYYY-MM-DD format.
How do I get this?
In bash (>=4.2) it is preferable to use printf's built-in date formatter (part of bash) rather than the external date (usually GNU date).
As such:
# put current date as yyyy-mm-dd in $date
# -1 -> explicit current date, bash >=4.3 defaults to current time if not provided
# -2 -> start time for shell
printf -v date '%(%Y-%m-%d)T\n' -1
# put current date as yyyy-mm-dd HH:MM:SS in $date
printf -v date '%(%Y-%m-%d %H:%M:%S)T\n' -1
# to print directly remove -v flag, as such:
printf '%(%Y-%m-%d)T\n' -1
# -> current date printed to terminal
In bash (<4.2):
# put current date as yyyy-mm-dd in $date
date=$(date '+%Y-%m-%d')
# put current date as yyyy-mm-dd HH:MM:SS in $date
date=$(date '+%Y-%m-%d %H:%M:%S')
# print current date directly
echo $(date '+%Y-%m-%d')
Other available date formats can be viewed from the date man pages (for external non-bash specific command):
man date
Try: $(date +%F)
The %F option is an alias for %Y-%m-%d
You can do something like this:
$ date +'%Y-%m-%d'
$(date +%F)
output
2018-06-20
Or if you also want time:
$(date +%F_%H-%M-%S)
can be used to remove colons (:) in between
output
2018-06-20_09-55-58
You're looking for ISO 8601 standard date format, so if you have GNU date (or any date command more modern than 1988) just do: $(date -I)
date -d '1 hour ago' '+%Y-%m-%d'
The output would be 2015-06-14.
With recent Bash (version ≥ 4.2), you can use the builtin printf with the format modifier %(strftime_format)T:
$ printf '%(%Y-%m-%d)T\n' -1 # Get YYYY-MM-DD (-1 stands for "current time")
2017-11-10
$ printf '%(%F)T\n' -1 # Synonym of the above
2017-11-10
$ printf -v date '%(%F)T' -1 # Capture as var $date
printf is much faster than date since it's a Bash builtin while date is an external command.
As well, printf -v date ... is faster than date=$(printf ...) since it doesn't require forking a subshell.
I use the following formulation:
TODAY=`date -I`
echo $TODAY
Checkout the man page for date, there is a number of other useful options:
man date
if you want the year in a two number format such as 17 rather than 2017, do the following:
DATE=`date +%d-%m-%y`
I use $(date +"%Y-%m-%d") or $(date +"%Y-%m-%d %T") with time and hours.
I used below method. Thanks for all methods/answers
ubuntu#apj:/tmp$ datevar=$(date +'%Y-%m-%d : %H-%M')
ubuntu#apj:/tmp$ echo $datevar
2022-03-31 : 10-48
Whenever I have a task like this I end up falling back to
$ man strftime
to remind myself of all the possibilities for time formatting options.
Try to use this command :
date | cut -d " " -f2-4 | tr " " "-"
The output would be like: 21-Feb-2021
#!/bin/bash -e
x='2018-01-18 10:00:00'
a=$(date -d "$x")
b=$(date -d "$a 10 min" "+%Y-%m-%d %H:%M:%S")
c=$(date -d "$b 10 min" "+%Y-%m-%d %H:%M:%S")
#date -d "$a 30 min" "+%Y-%m-%d %H:%M:%S"
echo Entered Date is $x
echo Second Date is $b
echo Third Date is $c
Here x is sample date used & then example displays both formatting of data as well as getting dates 10 mins more then current date.
You can set date as environment variable and later u can use it
setenv DATE `date "+%Y-%m-%d"`
echo "----------- ${DATE} -------------"
or
DATE =`date "+%Y-%m-%d"`
echo "----------- ${DATE} -------------"
echo "`date "+%F"`"
Will print YYYY-MM-DD
Try this code for a simple human readable timestamp:
dt=$(date)
echo $dt
Output:
Tue May 3 08:48:47 IST 2022

Resources