LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 11-28-2018, 09:59 AM   #31
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,998

Rep: Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338

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....
 
1 members found this post helpful.
Old 11-28-2018, 10:05 AM   #32
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by 1s440 View Post
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.
 
Old 11-29-2018, 02:13 AM   #33
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by l0f4r0 View Post
Please see my previous (edited) post (your problem is not sed related but it's about -eq instruction inside double brackets).
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"

Last edited by 1s440; 11-29-2018 at 02:43 AM.
 
Old 11-29-2018, 03:53 AM   #34
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Quote:
Originally Posted by 1s440 View Post
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

Last edited by l0f4r0; 11-29-2018 at 04:16 AM.
 
1 members found this post helpful.
Old 11-29-2018, 03:59 AM   #35
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by 1s440 View Post
´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.
 
Old 11-29-2018, 04:56 AM   #36
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by l0f4r0 View Post
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.
 
Old 11-29-2018, 04:57 AM   #37
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by berndbausch View Post
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.

Thanks for the reply
 
Old 11-29-2018, 05:01 AM   #38
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Quote:
Originally Posted by 1s440 View Post
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...
 
Old 11-29-2018, 05:54 AM   #39
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,770

Rep: Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933
Quote:
´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.
 
Old 11-29-2018, 06:21 AM   #40
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
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
 
Old 11-29-2018, 06:35 AM   #41
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by 1s440 View Post
Hi, I am using Debian 7. please let me know if there are some tools to monitor
Not sure if the Debian Wiki entry covers Debian 7, but there is no harm in trying.
 
Old 11-29-2018, 07:06 AM   #42
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,770

Rep: Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933
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
 
Old 11-29-2018, 08:16 AM   #43
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
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.

Hope its clear now
 
Old 11-29-2018, 08:29 AM   #44
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,720

Rep: Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973
Quote:
Originally Posted by 1s440 View Post
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.
 
Old 11-29-2018, 08:43 AM   #45
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by 1s440 View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script - incrementing a filename in a script tslinux Programming 10 08-05-2003 11:58 PM
can't run a script script from icon in konqueror scottsteibel Linux - Software 1 08-02-2003 07:59 PM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 05:34 AM
E-Mail notification to users via SMS (gateway script ok, but notification script?!?) Riku2015 Linux - Networking 10 03-08-2002 10:16 AM
Including methods from a perl script into another perl script gene_gEnie Programming 3 01-31-2002 05:03 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration