is there a way to have bash variable automatically updated upon calling? - bash

I would like to have a bash variable automatically updated. Let's say using a toy example:
now=`date`
echo $now
Say when I called it, it will be
Thu Sep 1 21:20:13 PDT 2016
The second time I call it (say using echo $now), I want $now to be updated to the current time instead of Thu Sep 1 21:20:13 PDT 2016, how to do it?

For that purpose create a function that prints the date:
now() {
date
}
And use it like this:
echo "This message is being printed on $(now)"

If you really want to go around that route, try below:
now="eval date"
echo "This message is being printed on $($now)"

Related

How to get the current date and time in specific format in Shell

I just want to get the system current time using below format in shell
Expected Format:
2019-02-14T08:08:12.300Z
I tried below piece of script but It returned something different.
CURRENTDATE=`date +"%Y-%m-%d %T"`
echo $CURRENTDATE
Output
2019-02-27 01:22:57
I just ran date command in my linux box and got below response back:
-bash-3.2$ date
Wed Feb 27 01:43:26 PST 2019
We are passing the above output as input JSON file. But this output is not accepted by our JSON. So I just want to pass the date and time format in the above specified format (Expected Format).
You may use:
dt=$(date '+%Y-%m-%dT%T.%zZ')
echo "$dt"
2019-02-27T04:35:56.-0500Z
Also note use of --iso-8601=seconds:
date --iso-8601=seconds
2019-02-27T04:37:29-05:00
You can also use this one like
currentDate=$(date +"%Y-%m-%dT%H:%M:%SZ")
echo $currentDate
OutPut :-
2022-07-12T18:05:27Z

How to find the date using internet (ie ntp) from bash?

How can I learn date and time from the internet using bash without installing anything extra.
I am basically looking for an equivalent of bash $ date, but using an NTP (or any other way) to get the correct date and time from the internet. All the methods I find (such as ntpd) are meant to correct the system time, which is not my purpose.
date has a lot of options for formatting, but I'm assuming that you just want the date and time:
ntpdate -q time.google.com | sed -n 's/ ntpdate.*//p'
(or any other time server)
If you have ntpd installed & configured then you can use the NTP Query command ntpq -crv which will return;
associd=0 status=04ff leap_none, sync_uhf_radio, 15 events, stale_leapsecond_values,
version="ntpd 4.2.6p5#1.2349-o Mon Feb 6 07:22:46 UTC 2017 (1)",
processor="x86_64", system="Linux/4.10.13-1.el6.elrepo.x86_64", leap=00,
stratum=1, precision=-23, rootdelay=0.000, rootdisp=1.000, refid=PPS,
reftime=dd2c9f10.f25911ee Wed, Aug 2 2017 19:57:20.946,
clock=dd2c9f11.f4251b0a Wed, Aug 2 2017 19:57:21.953, peer=6516, tc=4,
mintc=3, offset=-0.005, frequency=-17.045, sys_jitter=0.110,
clk_jitter=0.007, clk_wander=0.003, tai=37, leapsec=201701010000,
expire=201706010000
You want the line starting clock which gives the time, date etc - you would be best parsing this out with awk or something if you just want the date stamp rather then everything else.
You do not need to be a root user to run the command. It won't set anything, but will query your local server (presuming your running ntp) and present the details.

shell script to compare current system time with the time showing in an another output

I need to run curl using following URL and compare current system time with the time showing in the output. If current time is grater than 5 minutes than the output time then I need an email alert.
# curl -g http://new.abc.com/arcfilesync/cachejob?jobType=CACHEBUILD
null('{"status":"RUNNING","lastRunTime":"Mon Jun 13 2016 08:58:13 AM","lastRunStatus":"SUCCESS"}')
Can anyone guide me to write this?
Try this script:
#!/bin/bash
time1="Mon Jun 13 2016 08:58:13 AM";
timestamp1=$(date --date="$time1" +%s)
# create current date
time2=`date +%Y-%m-%d\ %H:%M:%S`
# Compute the timestamp
timestamp2=`date --date="$time2" +%s`
# get the difference between dates in seconds
let "diff=$timestamp2-$timestamp1"
# Compute the diff in minutes
let "diffInMin=$diff/60"
echo "diff $time1 <--> $time2"
echo "$diffInMin"
which will output the diff in minutes:
./getTimeDiff.sh
diff Mon Jun 13 2016 08:58:13 AM <--> 2016-06-13 11:13:36
135
If will create the timestamp from the time in your string and compare that to the current time. Now you only have to parse your curl request and get the time from there. I would use awk

How to suppress EOF when echoing messages to wall from a script

In my bash script, I use many echo "......." | wall lines to broadcast event notifications as they occur.
However, the resulting output on the console gets unwieldy:
Broadcast Message from root#BIGFOOT
(somewhere) at 16:07 ...
Photo backup started on Mon Oct 7 16:07:55 PHT 2013
Broadcast Message from root#BIGFOOT
(somewhere) at 16:08 ...
Photo backup successfully finished on Mon Oct 7 16:08:05 PHT 2013
Broadcast Message from root#BIGFOOT
(somewhere) at 16:08 ...
You may now unplug the Photo Backup HDD.
Instead, we'd like it to appear more like the following,
Broadcast Message from root#BIGFOOT
(somewhere) at 16:07 ...
Photo backup started on Mon Oct 7 16:07:55 PHT 2013
Photo backup successfully finished on Mon Oct 7 16:08:05 PHT 2013
You may now unplug the Photo Backup HDD.
which is kind of like what would appear in an open write chat session.
Is this possible? If so, how should I modify my script in order to achieve the desired console output?
Each wall invocation will add the "broadcast message" and blank newline at the top of your code.
As a result, if you want to notify your users at timely intevals (e.g. actually at the start + end of the backup) then you will have to live with the banner message.
As #devnull suggested, you could batch up the messages. One approach would be to declare a script wide variable say $logmsg and then have two functions depending on whether it is something you want the user to know eventually or something they want to know now
function log_message
{
$logmsg = "$logmsg\n$1"
}
function log_message_now
{
log_message "$1"
echo "$logmsg" | wall
logmsg = ""
}
(note I've not actually tested the above, so may need a touch of debugging!)
Use a compound command:
{
echo "line1"
echo "line2"
echo "line3"
} | wall

How can I format date -u so that the results include timezone offset in a Mac OSX terminal session?

In a terminal session I can use date -u to get
Mon Mar 16 03:34:39 2009 UTC
However, I'd like to include the offset. I'm modifying a TextMate tab trigger so that I can insert the full date including the local offset, in standard UTC format. I believe that would be in the following form:
Mon Mar 16 03:34:39 2009 UTC -0500
So, as you can see, I don't know how to get the timezone offset and combine that with formatted date results.
Try this:
echo `date -u` `date +%z`

Resources