[SOLVED] Synchronization of date between server and apache webserver, grep and sed instructions.
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
you ignored a lot of posts here:
1. do not use grep and sed together
2. date comparison will never work like this
Furthermore it is not a problem with sed. But actually it looks like there is an error with sed too, just you have forgotten to post that.
Finally please post your code within code tags, because it is really hard to read now.
The error with sed is that you use invalid char instead of '.
And there are other errors....
Hi Ignore the previous here is the correct but facing problems with sed
#!/bin/bash
date
VAR=$(curl -i http://google.com/ | grep Date | sed ‘s/Date: //‘ )
echo "´Today date is $(date)´"
echo "Output is $VAR"
if [[ $VAR -eq $(date) ]]; then
echo "´correct´"
else
echo "´wrong´"
fi
")syntax error in expression (error token is ": Wed, 28 Nov 2018 15:35:29 GMT
´wrong´
No problem with sed, but -eq tries to treat its arguments as numbers. Use the equals sign instead:
Code:
if [[ $VAR = $(date) ]]; then
This will still not work, because the Google date has a different format than the date command's default and probably uses a different time zone:
Code:
$ curl -i http://google.com/ 2>/dev/null | grep Date | sed 's/Date: //'; date
Wed, 28 Nov 2018 16:05:03 GMT
Thu Nov 29 01:05:03 JST 2018
You have to change the format of the date command.
Last edited by berndbausch; 11-28-2018 at 10:07 AM.
Yes, I have used [[ $VAR == $(date) ]]; but somehow the output i got as below. Have to change the date format and I have to remove the "Date: "
output from script:
´Today date is Thu Nov 29 09:07:05 CET 2018´
Output is Date: Thu, 29 Nov 2018 08:07:05 GMT
´wrong´
but the URL displays as Thu Nov 29 08:05:02 2018
Hope if we modify the curl output then it should be ok
curl -v --silent https://google.com/ 2>&1 | grep Date | sed -e 's/< Date: //'); date +"%d%m%Y%H%M%S"
Are you really sure you want to compare dates with a precision to 1second? If you do this, you will almost always have a no match because seconds will differ from the time you launched the 2 different commands...
Please change the thread title (you said you cannot but you didn't say what went wrong in the process), change grep <pattern> | sed <instructions> into sed '/pattern/{<instructions>}' and finally use [CODE] tags (don't expect any help from me next time if you keep not taking into account those remarks that have been told you multiples times before by different people).
I would do something like the following:
Code:
#!/bin/bash
myDate=$(date "+%a, %d %b %Y")
VAR=$(curl -i http://google.com/ | sed -n '/Date/s/Date: //p')
echo "Today date is $myDate"
echo "Output is $VAR"
if [[ ${VAR%% [[:digit:]][[:digit:]]:*} == $myDate ]]; then
echo "correct"
else
echo "wrong"
fi
NB: I let you deal with the different time zones (because it won't work as is). That's your exercise
´Today date is Thu Nov 29 09:07:05 CET 2018´
Output is Date: Thu, 29 Nov 2018 08:07:05 GMT
but the URL displays as Thu Nov 29 08:05:02 2018[/B]
Hope if we modify the curl output then it should be ok
I told you that the two formats are different. Rather than trying to modify the curl output, it's probably easier to use the TZ variable to ensure that both dates have the same time zone, then format the output of the date command.
Or, if you want a challenge, you parse the web page.
Are you really sure you want to compare dates with a precision to 1second? If you do this, you will almost always have a no match because seconds will differ from the time you launched the 2 different commands...
Please change the thread title (you said you cannot but you didn't say what went wrong in the process), change grep <pattern> | sed <instructions> into sed '/pattern/{<instructions>}' and finally use [CODE] tags (don't expect any help from me next time if you keep not taking into account those remarks that have been told you multiples times before by different people).
I would do something like the following:
Code:
#!/bin/bash
myDate=$(date "+%a, %d %b %Y")
VAR=$(curl -i http://google.com/ | sed -n '/Date/s/Date: //p')
echo "Today date is $myDate"
echo "Output is $VAR"
if [[ ${VAR%% [[:digit:]][[:digit:]]:*} == $myDate ]]; then
echo "correct"
else
echo "wrong"
fi
NB: I let you deal with the different time zones (because it won't work as is). That's your exercise
Hi,
Thanks for the reply. I have no negative intention in changing the title, I tried it did not work out. But somehow I struggled to change it because this is the first time I have edited the tile and added code tags. Thanks for the help.
I told you that the two formats are different. Rather than trying to modify the curl output, it's probably easier to use the TZ variable to ensure that both dates have the same time zone, then format the output of the date command.
Or, if you want a challenge, you parse the web page.
I have no negative intention in changing the title, I tried it did not work out. But somehow I struggled to change it because this is the first time I have edited the tile and added code tags.
Ok good but I don't have the feeling that your new title reflects very well what this whole thread is all about...
´Today date is Thu Nov 29 09:07:05 CET 2018´
Output is Date: Thu, 29 Nov 2018 08:07:05 GMT
but the URL displays as Thu Nov 29 08:05:02 2018
The first time is your system clock using your timezone information.
The second time is contained in header fields which is part of the HTTP protocol and not displayed in the browser.
The third time is something generated by the web page of whatever format as written.
Quote:
last week I had a issue with that where it was not updating the current date. so I am trying now to implement a check. So I thought once I develop a script it would be easy to create monitoring check.
As stated your going about this the wrong way and there are better tools to verify if your system clocked is synced. Unfortunately no one asked what you meant by "not updating the current date" or what distribution/version you are running.
The first time is your system clock using your timezone information.
The second time is contained in header fields which is part of the HTTP protocol and not displayed in the browser.
The third time is something generated by the web page of whatever format as written.
As stated your going about this the wrong way and there are better tools to verify if your system clocked is synced. Unfortunately no one asked what you meant by "not updating the current date" or what distribution/version you are running.
Hi, I am using Debian 7. please let me know if there are some tools to monitor
debian 7 still uses sysv init scripts. You can use the ntpdate command to check the time offset. You still have not fully explained the initial problem. You can use a pool or a specific time server.
debian 7 still uses sysv init scripts. You can use the ntpdate command to check the time offset. You still have not fully explained the initial problem. You can use a pool or a specific time server.
/usr/sbin/ntpdate -u -q 0.debian.pool.ntp.org
when I use "date" on the server its shows correct date and time
when i use "curl -i http://google.com" | sed -n '/Date/s/Date: //p' shows wrong time but correct date
but when I use http://google.com on the browser it shows me correct date and time
I want to create a monitoring check such that "date" command on the server should be same as the date on the browser "http://google.com/ if there is a mismatch then it should be an error.
when I use "date" on the server its shows correct date and time
when i use "curl -i http://google.com" | sed -n '/Date/s/Date: //p' shows wrong time but correct date
but when I use http://google.com on the browser it shows me correct date and time
I want to create a monitoring check such that "date" command on the server should be same as the date on the browser "http://google.com/ if there is a mismatch then it should be an error.
Hope its clear now
Clear, and totally pointless. If you have NTP running on your servers, that's all you need...it's ENTIRE PURPOSE is to keep your system clocks synced with the atomic clocks further upstream. That's all it does. Run NTP, and your clocks are synced.
when I use "date" on the server its shows correct date and time
when i use "curl -i http://google.com" | sed -n '/Date/s/Date: //p' shows wrong time but correct date
but when I use http://google.com on the browser it shows me correct date and time
I want to create a monitoring check such that "date" command on the server should be same as the date on the browser "http://google.com/ if there is a mismatch then it should be an error.
Hope its clear now
So you need to parse the google.com web site. Not a small feat if you want to use awk, sed etc., and you don’t know if the page’s format changes tomorrow.
In short, you are setting yourself up for failure, in my humble opinion.
By the way, I don’t see any date on google.com. Perhaps the output depends on the browser.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.