SUMMARY: Something useful

From: Michael Sullivan (mps@discomsys.com)
Date: Fri Apr 16 1999 - 16:29:10 CDT


Original Posting:

Michael Sullivan wrote:
>
> Here's something I put together mostly out of my laziness to set the
> DISPLAY variable back to my machine. I know the question comes up from
> time to time, and I have a very simple solution to this problem.
>
> Add the following to your .profile
>
> DISPLAY=`who -m | sed 's/.*(:*0*\.*0*\(.*\))/\1/'`:0.0
> export DISPLAY
>
> Works like a charm even when on your local machine. This might perhaps
> be a nice thing to add to the FAQ.
>
> Mike

I got the following responses from:

Nice thank you responses from....
Steve.Boronski@swift.stoke.gov.uk
Andrea Carleton <andrea@ll.mit.edu>
Bob Fulwiler <bobf@colltech.com>
David S. Foster <foster@bial1.ucsd.edu>
Stephan Grund <stephan.grund@isst.fhg.de> (With a minor glitch, fixed
below in the SUMMARY section.)

Works well, but I wish it could do this as well...
Jon LaBadie <jon@jgcomp.com> (I think I have a solution to this problem,
shown below in the SUMMARY section.)

Negative responses from...
Bevan Broun <bevanb@ee.uwa.edu.au>
Gene Rackow <rackow@mcs.anl.gov>

Individual responses below, my summary at the end:
(To skip directly to the summary section, search for: SUMMARY )

Steve.Boronski@swift.stoke.gov.uk
---------------------------------
Thank you Micheal, very useful

Steve Boronski

Andrea Carleton <andrea@ll.mit.edu>
-----------------------------------
This is useful indeed! I am adding it ( slightly changed for
csh use) to all my accounts as we speak.

Thanks for posting.

Bob Fulwiler <bobf@colltech.com>
--------------------------------
Good Morning,

Certainly is useful! I used it this am.
Thanks

David S. Foster <foster@bial1.ucsd.edu>
--------------------------------------
This is pretty slick! I had been using $REMOTEHOST in the TCSH but that
seemed a bit flaky sometimes.

Stephan Grund <stephan.grund@isst.fhg.de>
-----------------------------------------
Hi!

Really, a nice little thing. But I have some remarks.
I'm sitting for a X-Terminal. In a shell of the machine, the terminal
is connected to, I get:

#who -m
sgrund pts/0 Apr 16 08:06 (capricorn )

and therefor

#echo `who -m | sed 's/.*(:*0*\.*0*\(.*\))/\1/'`:0.0
capricorn :0.0

Not quite right ;-(

with a rlogin:

#rlogin deer
#who -m
sgrund pts/16 Apr 16 11:26 (tern)

#echo `who -m | sed 's/.*(:*0*\.*0*\(.*\))/\1/'`:0.0
tern:0.0

I've tried to fix the first problem (delete the space), but I have to
admit, that I don't 'understand' the .*(:*0*\.*0*\(.*\))/\1 fully ;-(
The second problem ('wrong' hostname via rlogin) is of course not Your
fault.

Jon LaBadie <jon@jgcomp.com>
----------------------------
I use the same basic code.

It fails when, as I do often, I go from machine A to B then to C.
On C it is set to display on B when A is the "real" display.

Bevan Broun <bevanb@ee.uwa.edu.au>
---------------------------------
Doesnt works on solaris-2.4, irix-6.5, irix-5.3, DigitalUnix-4.0.
Works on linux. Solaris-2.5,Solaris-2.7

I use ssh which sets all permissions and the DISPLAY variable.

Gene Rackow <rackow@mcs.anl.gov>
--------------------------------
I hate to tell you this, but that is a rather poor solution in that
it does not work on very many machines. It does not work on linux, SunOS4,
AIX, or IRIX. If you have a shared filesystem, your really out of luck.
A much better solutionis to use SSH to do your logins which will take care
of the clear-text passwd problem AND automaticly set your display as well.

SUMMARY
-------
First, let me thank the people who sent the nice thank you responses, and
I am glad I could help you out.

Secondly, let me address the negative responses.

Yes, I am aware of the capabilities of ssh and what it can do, however
not everyone is able to run ssh for one reason or another. Some work
for companies which restrict the use of "Open Source", "Freeware", or
"GNU Tools"

The really nice thing about my approach is that the solution works
equally well with ssh, telnet, and rlogin, or just about any other login
method you may use. What I'm saying here is that it cannot hurt, even
if you use ssh.

I cannot address the issue of IRIX, AIX, Solaris 2.4, DGUX, OSF, etc. I
do not have access to those machines, however with a bit of tweaking to
the DISPLAY setting line, I'm sure it is capable of working on those
other systems. A bit of creativity would be called for, but I'm sure it
is not impossible. Perhaps something like this, and bear in mind it's
all speculation and untested.

#get os type and version - on Solaris the following should work:
#might be something else on another OS.
osname=`uname -s`
osrel=`uname -r`

case ${osname} in
   SunOS )
      case ${osrel} in
         5.5.1|5.6|5.7 )
            gethost_cmd=`who -m`
            regex="'s/.*(:*0*\.*0*\(.*\))/\1/'"
            ;;
         5.4 )
            gethost_cmd=`solaris 2.4 machine info command`
            regex="'fill in the regex that works with 2.4'"
            ;;
         4* )
            # Perhaps it might be time to upgrade? Who knows how much
            # life Sun will continue to breathe into SunOS 4.x.x.?x.
            gethost_cmd=`SunOS 4.x.x.?x machine info command`
            regex="'fill in the regex that works with 2.4'"
            ;;
      ;;

      # I'm guessing at the others here...
   AIX )
      For best results, wash rinse, repeat... (for each version you
      support)
      ;;

   Irix )
      For best results, wash rinse, repeat... (for each version you
      support)
      ;;

   Linux )
      For best results, wash rinse, repeat... (for each version you
      support)
      ;;
esac

DISPLAY=`${gethost_cmd} | sed ${regex}`:0.0; export DISPLAY

exit

You see this could get quite complicated, but this method can be
supported by multiple OS types and versions.

(NOTE: the two critical responses disagree on my previous method working
on Linux. One says yes and the other says no.)

Third, to address Stephan Grund's <stephan.grund@isst.fhg.de> minor
glitch when using an X-Terminal, I offer the following fix to the sed
script.

DISPLAY=`who -m | sed '{
      s/.*(:*0*\.*0*\(.*\))/\1/
      s/ //g
   }'`:0.0;
export DISPLAY

I know this could probably be simpler, but the quick hack of adding the
other sed command in a command group takes care of the extraneous
space.

Fouth, Jon LaBadie <jon@jgcomp.com> raises an interresting point about
going from A to B then C and wanting the display set back to A on C. I
also find myself in his position as well. This situation cannot even be
handled by ssh. I'm sure there is a solution lurking out there
somewhere, and if anyone has a good one, pass it along. I'll test it,
and pass it along to all.

It's an interesting challenge anyway, and never being one to shy away
from a challenge, I offer the following:

# add the following to your .profile, csh users you probably know how to
# modify this. This may have some unwanted side effects on the local
# machine. because you might get your display set to localnostname:0.0
# I haven't tested this thoroughly, but it works fairly well so far.
# It MAY get you hosed if you are logged into two machines
# simultaneously from the console devices. (Hey no one ever does that...
# *grin* )

DISPLAY=`who -m | sed 's/.*(:*0*\.*0*\(.*\))/\1/'`:0.0
# Or this if you have the X-Terminal problem described above.
#DISPLAY=`who -m | sed '{
# s/.*(:*0*\.*0*\(.*\))/\1/
# s/ //g
# }'`:0.0

if [ "${DISPLAY}" = ":0.0" ]
then
        # YMMV - whatever is the equiv to uname -n to get the hostname of
        # your local machine.
        echo `uname -n`:0.0 > ${HOME}/.current.display.term
else
        DISPLAY=`cat ${HOME}/.current.display.term`
fi
export DISPLAY

I am actually using this now in my .profile, again csh people can
probably figure out how to convert.

Fifth, I'd like to explain how the regexp works for those who don't
quite understand it.

s/.*(:*0*\.*0*\(.*\))/\1/

1. The .*( matches beginning with one or zero characters preceeding a
    "(". The "." matches any character, and its modifier * means zero or
    more occurrances of the preceeding regexp, or character.

2. The :* matches zero or more occurrances of a ":" after the "("
    Same thing for the "0" the "." has to be escaped with a "\"
    otherwise the regexp would think match on any character.

3. The "\(" and the corresponding "\)" create a reference to the regexp
    contained within them, in this case any zero or more occurrances of
    any characters. This is important, because the result will be null
    if we previously matched on a :0.0 and that prevents having :0.0:0.0
    set in the DISPLAY.

4. The closing ) is simply that, remember we're looking for a string
    bounded by "()" like (mymachine.domain.com) or (:0.0) for a
    localhost.

5. The "/" is a delimiter for the entire substitution.

6. The "\1" is a back reference to the regular expression containd in
    the "\(" and "\)"

I hope that's clear. If anyone needs further explaination, let me
know.

Lastly, here's another fun toy for sed. I cannot clame authorship for
this, but I think it's sort of fun.

alias ccal='cal|sed -es/\$/\ / -es/^/\ / -e"s/ \(`date +%e%n`\) /#\1#/"'
(NOTE: this is for ksh and has been tested on Solaris 2.4, 2.5, 2.5.1,
2.6, and Solaris 7.)

Mike

-- 
Michael P. Sullivan                                  
Distributed Computing Systems, LLC                  Cell: 516-429-2080 
E-Mail: mps@discomsys.com                    http://www.discomsys.com/ 
* UNIX Systems and Database Consulting, Architecture and Management  *
"Any sufficiently advanced technology is indistinguishable from magic"
                                   A. C. Clarke



This archive was generated by hypermail 2.1.2 : Fri Sep 28 2001 - 23:13:18 CDT