start page | rating of books | rating of authors | reviews | copyrights

Unix Power ToolsUnix Power ToolsSearch this book

3.12. Terminal Setup: Testing Remote Hostname and X Display

If you log in from other hosts (Section 1.21) or from hosts running the X Window System (Section 24.20), the who am i[8] command will probably show a hostname and/or window information in parentheses:

[8]Also try "who mom likes" or maybe "who is responsible?" -- the who doesn't really care, as long as there are only two arguments. So, "who let the dogs out?", as you might expect, causes an error.

schampeo@fugazi:1002 $ who am i
schampeo ttyp7    Jun 19 03:28 (fugazi:0.0)

(Long hostnames may be truncated. Also, note that some versions of who am i prepend the name of the local host to the username and don't include the remote hostname at all in their output. Check yours before you write this test.) The information in parentheses can help you configure your terminal based on where you've logged in from and/or which display you're using. To test it, add commands such as the following to your .profile file. (In C-type shells, use a switch statement in .login instead.)

case Section 35.10

case "`who am i | sed -n 's/.*(\(.*\))/\1/p'`" in 

\(..\) \1 Section 34.11

*0.0)   ...do commands for X display 0 ;;
mac2*)  ...do commands for the host mac2.foo.com ;;
"")     ...no output (probably not a remote login) ;;
*)      ...do commands for other situations ;;
esac

That uses sed (Section 34.1) to give the text between the parentheses for that remote host to the case. This *0.0 case matches lines ending with 0.0; the mac2 case matches lines that start with mac2; an empty string means sed probably didn't find any parentheses; and the * case catches everything else.

--JP and SJC



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.