LinuxQuestions.org
Review your favorite Linux distribution.
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, 05:17 AM   #16
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936

If your server was not showing the correct date then something could be wrong with your server or its internet connection or how ntp is configured.

Code:
while read var value
do
"$var"= curl -XPUT 'http://google.com-'$date'
done
You have many syntax errors with your loop. And while we could fix the errors if the server and ntp is configured and running correctly you should not have a date problem.

Last edited by michaelk; 11-28-2018 at 05:49 AM.
 
Old 11-28-2018, 05:43 AM   #17
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
If NTP is working correctly, you get the best possible date from the internet already.

But if you want it from a URL, here is a solution:
Code:
curl -i https://google.com 2>/dev/null | grep Date | sed ‘s/Date: //‘
Wed, 28 Nov 2018 11:36:09 GMT
The grep command selects the line that contains the Date header. The sed command removes the string “Date: “.
 
Old 11-28-2018, 08:03 AM   #18
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,039

Rep: Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347
Off topic, but do not use grep|sed chains (or similar) if not really needed:
Code:
curl -i https://google.com 2>/dev/null | grep -o -P '(?<=Date: ).* ...'
curl -i https://google.com 2>/dev/null | sed -n '/Date/{s/Date: //;p}'
 
1 members found this post helpful.
Old 11-28-2018, 08:25 AM   #19
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
@1s440: can you please change your thread title (currently: "Script") into something much more specific? (HOWTO: select a new title by editing your thread original post)
 
Old 11-28-2018, 08:33 AM   #20
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by berndbausch View Post
If NTP is working correctly, you get the best possible date from the internet already.

But if you want it from a URL, here is a solution:
Code:
curl -i https://google.com 2>/dev/null | grep Date | sed ‘s/Date: //‘
Wed, 28 Nov 2018 11:36:09 GMT
The grep command selects the line that contains the Date header. The sed command removes the string “Date: “.
Hi Thanks for the reply. This work as ntp is configured correctly.

Last edited by 1s440; 11-28-2018 at 08:38 AM.
 
Old 11-28-2018, 08:38 AM   #21
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by l0f4r0 View Post
@1s440: can you please change your thread title (currently: "Script") into something much more specific? (HOWTO: select a new title by editing your thread original post)
hi Unable to change the thread title. sorry
 
Old 11-28-2018, 08:39 AM   #22
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Quote:
Originally Posted by 1s440 View Post
Hi Thanks for the reply. This work as ntp is configured correctly. Then don't need a script.
Then please mark your thread as [SOLVED]

Quote:
Originally Posted by 1s440 View Post
hi Unable to change the thread title. sorry
What doesn't work with the HOWTO I provided to you?

Last edited by l0f4r0; 11-28-2018 at 08:43 AM.
 
Old 11-28-2018, 09:07 AM   #23
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 pan64 View Post
Off topic, but do not use grep|sed chains (or similar) if not really needed:
Code:
curl -i https://google.com 2>/dev/null | grep -o -P '(?<=Date: ).* ...'
curl -i https://google.com 2>/dev/null | sed -n '/Date/{s/Date: //;p}'
This is very useful, but the curly braces in the sed command are a bit too baroque for my taste. What’s wrong with
Code:
sed -n ‘/^Date/s/Date: //p’
?

And I refuse to learn Perl.

Last edited by berndbausch; 11-28-2018 at 09:09 AM.
 
1 members found this post helpful.
Old 11-28-2018, 09:19 AM   #24
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Quote:
Originally Posted by berndbausch View Post
This is very useful, but the curly braces in the sed command are a bit too baroque for my taste. What’s wrong with
Code:
sed -n ‘/^Date/s/Date: //p’
?
I didn't know about the p flag in substitution command, thank you!
Actually, this doesn't change anything in this simple example. So well done
But braces are still required for more advanced use-cases when you need to nest instructions in relation to a /pattern/...

Last edited by l0f4r0; 11-28-2018 at 09:30 AM.
 
Old 11-28-2018, 09:19 AM   #25
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by berndbausch View Post
This is very useful, but the curly braces in the sed command are a bit too baroque for my taste. What’s wrong with
Code:
sed -n ‘/^Date/s/Date: //p’
?

And I refuse to learn Perl.
Code:
#!/bin/bash
date
VAR=$(curl -i http://google.com/ | grep Date)
echo "Today date is "$date""
echo "Output is "$VAR""
if [ $VAR -eq $date ]; then
echo "date is correct"
else
echo "date is wrong"
fi

Last edited by 1s440; 11-29-2018 at 04:49 AM.
 
Old 11-28-2018, 09:34 AM   #26
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Quote:
Originally Posted by 1s440 View Post
#!/bin/bash
date
VAR=$(curl -i http://google.com/ | grep Date)
echo "Today date is "$date""
echo "Output is "$VAR""
if [ $VAR -eq $date ]; then
echo "date is correct"
else
echo "date is wrong"
fi
-eq is a numerical comparison so you need surrounding double parentheses ((. But I don't think it's gonna work because date returns a string... So you may want [[ $VAR == $date ]] instead (if I were you I would prefer using double square brackets ([[) instead of single bracket command ([) in your bash scripts).
Where is your date variable initialized?
What makes you think it answers berndbausch's question?
And finally, please use [CODE] tags.

Last edited by l0f4r0; 11-28-2018 at 09:55 AM.
 
Old 11-28-2018, 09:41 AM   #27
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936
A direct comparison of the two variables will never be the same.

The header is in UTC(GMT) so you need to use the date -u command. In addition time will never match and the format is different. Are you trying to verify the day, time or both?
 
Old 11-28-2018, 09:46 AM   #28
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by 1s440 View Post
Hi Thanks for the reply. This work as ntp is configured correctly.
Unfortunately sed is not working little complicated
 
Old 11-28-2018, 09:48 AM   #29
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by l0f4r0 View Post
If you are writing a bash script, then if I were you I would prefer using double square brackets ([[) instead of single bracket command ([).
Where is date variable initialized?
What makes you think it answers berndbausch's question?
And finally, please use [CODE] tags.
Hi Ignore the previous here is the correct but facing problems with sed
Code:
#!/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´

Last edited by 1s440; 11-29-2018 at 04:50 AM.
 
Old 11-28-2018, 09:58 AM   #30
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
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´
Please see my previous (edited) post (your problem is not sed related but it's about -eq instruction inside double brackets).
 
  


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 07:55 AM.

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