[K12OSN] Re: Need script help for fl_teachertool

Eric Harrison eharrison at mail.mesd.k12.or.us
Wed May 3 02:33:40 UTC 2006


On Tue, 2 May 2006, Robert Arkiletian wrote:

> On 5/2/06, Robert Arkiletian <robark at gmail.com> wrote:
>> I can find the ip of a client I am sitting at with
>> 
>> cat /etc/hosts | grep `echo $DISPLAY | cut -d: -f1` | cut -f1
>> 
>> problem is if I run this command from the server I get
>> 
>> Usage: grep [OPTION]... PATTERN [FILE]...
>> Try `grep --help' for more information.
>
> Found a solution.
>
> cat /etc/hosts | grep -w `echo $DISPLAY | sed -e 's/:/ /g' | awk
> '{print $1}'` | cut -f1
>
> which returns 127.0.0.1 and that works in my code.

Same problem as the earlier code, just by luck it happens to give the
answer you were expecting  (took me a little while to figure out why ;-)

If you run the sub command (echo $DISPLAY |sed -e 's/:/ /g'|awk '{print $1}')
by itself, you'll see that the output is "0.0".  "0.0" just happens to match
"127.0.0.1", even when you use the "-w" with grep:

 	# echo 127.0.0.1 | grep -w 0.0
 	127.0.0.1

The trick is that grep considers "." as a "non-word constituent character".
Note the last sentence from that section of the grep man page:

        -w, --word-regexp
               Select only those  lines  containing  matches  that  form  whole
               words.   The  test is that the matching substring must either be
               at the beginning of the line, or preceded  by  a  non-word  con-
               stituent  character.  Similarly, it must be either at the end of
               the line or followed by a non-word constituent character.  Word-
               constituent  characters are letters, digits, and the underscore.

Thus anything with ".0.0." in it will match "0.0".


This should work better for you:

 	getent hosts | grep ^`echo $DISPLAY | sed -e 's/:/ /g'\   | awk '{print $1}'` | cut -f1


BUT, this is all very convoluted. It has the feeling of approaching a problem
in the wrong way.  What is the context of what you are working on? There may
be a cleaner solution.

> I'm wondering are the client hostnames of
>
> ws253.ltsp
> ws252.ltsp
> ws251.ltsp
> etc...
>
> always
>
> 192.168.0.253
> 192.168.0.252
> 192.168.0.251
> etc...

Those are just the defaults, they can be changed.

-Eric




More information about the K12OSN mailing list