Technical article 109064

How can I execute a program based on the user's login tty device?

OpenServer 5.0.0

Written · Last revised

Problem

I would like to set a system-wide environment variable that is based upon the login device assigned to the user.

-or-
I would like to have my users execute a different script
depending on which tty device the user logged in on.  How
do I execute a program based upon the user's tty login device?

Solution

First, the dependency in these problems is the login device
that has been assigned to the user.  In order to execute a
program, or set a variable, based upon the login device,
implement this shell script stub:
TTY=`tty`
TTY=`basename "$TTY"`
The name of the tty login device has been stored in the
local variable, TTY.  The next step is to invoke the case
construct against the TTY variable to evaluate the contents
of the variable and then take a specific action:
case "$TTY" in
   tty0? | tty1? ) echo "at console keyboard!" ;;
   ttyp?         ) echo "at pseudo tty 0-9" ;;
   ttyp0[1-9]    ) echo "at pseudo tty > 9" ;;
   *             ) echo "serial port default" ;;
esac
In the example above, a string is printed to the screen
based on the device used as the login device.  It would be
just as easy to substitute the echo statement for a variable
assignment or to invoke an application.

Next, determine where to insert your shell code stub. If you are simply setting environmental variables for all users on your system, then it would be appropriate to edit the /etc/profile master file.

If your changes will apply only to a limited group
of users you could change the individual run-command scripts
(.kshrc, .cshrc, .login, etc.) for each user.  Of course,
if you are controlling the login environment by exec'ing
an application directly from the run-command scripts, then
the user would be free to modify or change your code.

A last option would be to place your code stub within your application which may be called from the run-command scripts. This would mean that your shell script stub would execute only if accessing your custom application.

From the Xinuos technical article database, record 109064. Reproduced with permission. The original is at www.sco.com/ta/109064, which is reachable but blocked from search engines. Xinuos marks this article against a retired product line. That refers to the product’s lifecycle, not to whether the advice still works — plenty of it still applies to running systems.

Next step

Need this fixed rather than explained?

Tell us the release, what the system runs and what it is doing. We will reply by email with what we would do next.

  • You deal with the people who do the work
  • No obligation, and no upgrade required before we will help
  • We tell you plainly if something is not worth doing

Send an enquiry

We reply by email. If a server is down, say so in the first line. You can also write to help@scoexpert.com.