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" ;;
esacIn 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.