UNIX Hints & Hacks |
|||||||||||||||||||||||||||||||||||||
Chapter 5: Account Management |
|
||||||||||||||||||||||||||||||||||||
|
A little hack that determines where your display actually is.
Flavor: BSD
Shells: bash, ksh, sh
Do you work in a large networked environment and find yourself frequently running rlogin or telnet across the network? Are you always annoyed by constantly needing to set the DISPLAY variable every time you want to point at the X display of the machine you are working on? Put the following lines into the .profile of your startup file in your home directory to automatically set the DISPLAY variable for you:
if [ "$DISPLAY" = "" ]; then if [ "`tty`" != "/dev/console" -o "$TERM" != "console" ]; then REMOTEHOSTNAME="`who am i | cut -d'(' -f2,2 | cut -d')'-f1,1`" DISPLAY="$REMOTEHOSTNAME:0" export DISPLAY fi fi
Line 1: Check whether the $DISPLAY has no value and continue.
Line 2: If the tty is not the console device and the terminal doesn't equal the console, continue.
Line 3: Get the name of the host you came in from. This is determined by taking the output of the who am I command and outputting the hostname from what's between the parenthesis.
Line 4: Set the DISPLAY to the system you came from.
Line 5: Exit.
As you log in to a local workstation, the $DISPLAY variable is checked to see whether it has no string defined. If there is none, the routine then checks whether you are local to the console. In this case you are local and nothing is done. If you telnet into a remote host, the $DISPLAY variable is checked to see whether it has a string defined. If there is no string defined, the routine checks to see whether you are local to the console. In this case you are not, so the routine does a who am i. This command outputs information about who is logged in, the terminal device, a time stamp, and the reverse name lookup of the machine that you are coming from:
rocket 3% who am i roger pts/5 Nov 2 07:39 (plane)
It then cuts the output from this command and collects only the hostname of the machine that you're coming from (in parentheses). The DISPLAY variable is then set to this host and exported as a global variable.
If you get a chance to work at a large installation, you tend to bounce around logging in to one machine after another as you support various users on multiple platforms. It is nice not having to set the DISPLAY variable every time.
The nice thing about having the DISPLAY set at startup like this is that you can log in from any machine on the network. As you saw earlier (in section 5.10, "Using Aliases"), an alias can be set up to set the DISPLAY variable on your most common machine.
alias setrocket 'setenv DISPLAY rocket:0' # Display X back on rocket
The only bad thing about this is that you need to have an entry for each of the systems you plan to log in from on the remote systems.
UNIX Hints & Hacks |
|||||||||||||||||||||||||||||||||||||
Chapter 5: Account Management |
|
||||||||||||||||||||||||||||||||||||
|
© Copyright Macmillan USA. All rights reserved.