home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 2.11 Faster ksh and bash Startup with $- Test Chapter 2
Logging In
Next: 2.13 A .cshrc.$HOST File for Per Host Setup
 

2.12 Automatic Setups for Different Terminals

If you work at several kinds of terminals, terminal setup can be tough. For instance, my X terminal sends a backspace character when I push the upper-right key, but the same key on another terminal sends a delete character-I want stty erase (5.9 ) to set the right erase character automatically. Maybe you want a full set of calendar programs started when you log in to the terminal at your desk, but not when you make a quickie login (2.5 ) from somewhere else.

Here are some ideas for changing your login sequence automatically. Some examples are for the C shell and use that shell's switch (47.6 ) and if (47.3 ) . Examples for Bourne-type shells use case (44.5 ) and if (44.8 ) . If you use the other type of shell, the idea still applies; just switch the syntax.

  • If all you want to do is initialize your terminal (set the TERM variable (5.10 ) , set your erase character, etc.), the tset (5.3 ) command may be all you need.

  • If your TERM environment variable is set differently on each terminal, you can add a test like this to your .login file:

    switch ($TERM)
    case vt100:
       ...do commands for vt100
    
       breaksw
    case xxx
    
    :
       ...do commands for xxx
    
       breaksw
    default:
       ...do commands for other terminals
    
       breaksw
    endsw

    and so on.

  • If you log in from other hosts (1.33 ) or from hosts running the X window system (1.31 ) , the who am i command will probably show a hostname and/or window information in parentheses:

    bash$ who am i
    
    
    jpeek    pts/6        Jul 17 10:30   (www.jpeek.com:0.0)

    (Long hostnames may be truncated. Check yours before you write this test.) If the information in parentheses will help, add commands like these to your .profile file:

    case
     \(..\)  \1
     
    
    
    
    
    case "`who am i | sed -n 's/.*(\(.*\))/\1/p'`" in
    *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 (34.24 ) 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.

  • If you know that certain port numbers are used for certain kinds of logins, you can test that. For example, many systems use ttyp0 , ttyq1 , etc. as network ports for rlogin and telnet (1.33 ) . This test will branch on the port name:

    case "`tty`" in
    /dev/tty[pqrs]?)
       # rlogin, telnet:
       ...
    /dev/tty02)
       # terminal on my desk:
       ...
    "not a tty") ;;  ...not a terminal login session; do nothing
    
    esac

  • Certain systems set certain environment variables. For example, the X Window System sets a DISPLAY environment variable. (If you aren't sure about your system, use the env or printenv command (6.1 ) to look for changes in your environment at different systems.) You can test that:

    if
     $?
     
    
    
    
    
    
    
    
    if ($?DISPLAY) then
       # on X window system
       ...
    else if ($?WIN_PARENT) then
       # on SunView system
       ...
    else
       ...
    endif

  • Your system may have a /etc/ttytab or /etc/ttys file that lists the type of each terminal port. Lines in the file look something like this:

    console "/usr/etc/getty std.9600"    vt100      on  local 
    tty00   "/usr/etc/getty std.9600"    dialup     off local
    tty01   "/usr/etc/getty std.9600"    plugboard  off local
       ...
    ttyp0   none                         network    off
       ...

    (For example, port ttyp0 is network , the type used by xterm (1.31 ) , telnet (1.33 ) , and so on.)

    You can match the output of the tty (3.8 ) command, which shows your current tty, to the first column of that file. The output of tty starts with /dev or /dev/pts . So, to match your current tty to the file, you need to strip the name to its tail. For example, in bash and ksh , these three lines would put the terminal port type (vt100 , plugboard , etc.) into the ttykind shell variable:

    
${..#..}
     
    awk
     
    tty=`tty`
    ttytail=${tty#/dev/}
    ttykind=`awk '$1 == "'$ttytail'" {print $3}' /etc/ttys`

  • You can also deal with many of these cases using the venerable but obscure tset (5.3 ) program to select and initialize the correct terminal type. Another program you can use to set the terminal type is qterm (5.5 ) , available on the CD-ROM.

- JP


Previous: 2.11 Faster ksh and bash Startup with $- Test UNIX Power Tools Next: 2.13 A .cshrc.$HOST File for Per Host Setup
2.11 Faster ksh and bash Startup with $- Test Book Index 2.13 A .cshrc.$HOST File for Per Host Setup

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System