LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-06-2006, 06:47 PM   #1
dosnlinux
Member
 
Registered: Mar 2005
Distribution: slackware 11, arch 2007.08
Posts: 154

Rep: Reputation: 30
Question home-grown ntp


I want to write a script to set the time from time.gov, but the date command doesn't want to read the results of the script to fetch the time.

contents of the myntp.sh

Code:
wget -o /dev/null http://www.time.gov/timezone.cgi?Eastern/d/-5 -O - | html2text | grep ..\:.. | sed 's/ //g'
I've tried using pipes, backquotes, and input redirection from a file.

Code:
./myntp.sh | date --set=

date --set=`./myntp.sh`

./myntp.sh > /tmp/time
date --set=</tmp/time
Is there a way to make this work?
 
Old 08-06-2006, 07:11 PM   #2
tangle
Senior Member
 
Registered: Apr 2002
Location: Arbovale, WV
Distribution: Slackware
Posts: 1,761

Rep: Reputation: 78
Would something like this work?
/usr/sbin/ntpdate 128.118.25.3; hwclock --systohc
 
Old 08-06-2006, 07:14 PM   #3
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
Have you tried breaking up the commands in the script up and using echo statements (and use /bin/bash -x as the interpreter) to see what each step is returning? The individual commands look OK, although I'd use the following to grep the output:
Code:
grep -E '[[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}'
As an alternative, can you use ntpdate?
 
Old 08-06-2006, 07:50 PM   #4
dosnlinux
Member
 
Registered: Mar 2005
Distribution: slackware 11, arch 2007.08
Posts: 154

Original Poster
Rep: Reputation: 30
Wow, two posts in less than an hour! Tangle the ntp command worked great. gilead, I'm a bit confused about the individual commands and echo part, but here's what I get running the commands individually then adding the next command to the pipe:

The wget part outputs the raw html code
html2txt formats the raw html into what the page looks like when viewed in links
grep returned " hh:mm:ss" (filled in with the time)
(when I tried grep -E '[[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}' I got ""
and sed just got rid of the extra spaces

The error messages I got with my date commands are:

date: invalid date `2\b20\b0:\b:4\b47\b7:\b:4\b44\b4'
and with sh -x: /bin/date: /bin/date: cannot execute binary file

In the future I think I will use ntpdate, but I'm still interested in why date isn't accepting my input.

Last edited by dosnlinux; 08-06-2006 at 07:51 PM.
 
Old 08-06-2006, 07:52 PM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
I ran your first script like this:

wget -o /dev/null http://www.time.gov/timezone.cgi?Eastern/d/-5 -O - | html2text | grep ..\:.. | sed 's/ //g' | od -c
0000000 2 \b 2 0 \b 0 : \b : 4 \b 4 7 \b 7 :
0000020 \b : 2 \b 2 0 \b 0 \n
0000031

Does the backspacing and overtyping screw up your results?

wget -o /dev/null http://www.time.gov/timezone.cgi?Eastern/d/-5 -O - | html2text | grep ..\:.. | sed -e 's/ //g' -e 's/^B.//'
 
Old 08-06-2006, 08:16 PM   #6
dosnlinux
Member
 
Registered: Mar 2005
Distribution: slackware 11, arch 2007.08
Posts: 154

Original Poster
Rep: Reputation: 30
nope, wget -o /dev/null http://www.time.gov/timezone.cgi?Eastern/d/-5 -O - | html2text | grep ..\:.. | sed -e 's/ //g' -e 's/^B.//' still gives the same results

but the od -c give different results than your does when I enter wget -o /dev/null http://www.time.gov/timezone.cgi?Eastern/d/-5 -O - | html2text | grep ..\:.. | sed 's/ //g' | od -c (is this is expected?)

output of my od -c
Code:
0000000   2  \b   2   1  \b   1   :  \b   :   1  \b   1   2  \b   2   :
0000020  \b   :   4  \b   4   0  \b   0  \n
0000031
output off diff tmp tmp2 (tmp2 is copied from jschiwal's post)
Code:
1,2c1,2
< 0000000   2  \b   2   1  \b   1   :  \b   :   0  \b   0   8  \b   8   :
< 0000020  \b   :   1  \b   1   8  \b   8  \n
---
> 0000000 2 \b 2 0 \b 0 : \b : 4 \b 4 7 \b 7 :
> 0000020 \b : 2 \b 2 0 \b 0 \n

Last edited by dosnlinux; 08-06-2006 at 08:18 PM. Reason: needed preformated text
 
Old 08-06-2006, 08:44 PM   #7
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
Thank you both for pointing out the weird output. I didn't have html2text installed so I couldn't see anything wrong with the grep I posted earlier. I installed it, got the control codes, etc. and breathed a sigh of relief...
 
Old 08-06-2006, 11:20 PM   #8
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
For the control character in the sed filter "-e 's/^B.//', I typed it by pressing Cntrl-v Cntrl-b. It isn't the literal characters '^B'. Nevertheless, it's wrong all the same. It should be [Cntrl]-v[Cntrl-h.
And the substitution should be done globally on the line.
"-e 's/^H//g'"

wget -o /dev/null http://www.time.gov/timezone.cgi?Eastern/d/-5 -O - | html2text | grep ..\:.. | sed -e 's/ //g' -e 's/'^H'.//g' | od -c
0000000 0 0 : 2 0 : 0 4 \n
0000011
 
Old 08-07-2006, 12:23 AM   #9
dosnlinux
Member
 
Registered: Mar 2005
Distribution: slackware 11, arch 2007.08
Posts: 154

Original Poster
Rep: Reputation: 30
It worked! I just added backquotes to the '^H' (typed as ctrl+v, ctrl+h)version of the script and it corrected the clock. I guess I need to brush up on my control and escape characters

Thanks
 
Old 08-07-2006, 03:58 AM   #10
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
The [Cntrl]-v [Cntrl]-<key> sequence also works in vim.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Grown-up Linux in the Datacenter LXer Syndicated Linux News 0 05-27-2006 06:33 AM
"...the number of UNIX installations has grown to 10, with more expected..." AnanthaP General 0 02-14-2006 07:21 AM
mqueue has grown too large Pete Dogg Linux - Software 1 10-29-2004 09:10 AM
NTP cannot work with timeserver, NTP-d can jerryvapps Linux - Networking 0 08-04-2004 02:04 PM
NTP cannot use server, NTP -d can jerryvapps Linux - Newbie 0 07-28-2004 02:22 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:36 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