Previous Table of Contents Next


Module 144
tty

DESCRIPTION

The external tty command displays the name of your terminal device path. Your terminal device path is used by UNIX to communicate with your physical terminal. The tty command does not display your terminal type.

COMMAND FORMAT

Following is the general format of the tty command.

     tty [ -ls ]

BSD (Berkeley)
tty [ -s ]

Options

The following list describes the options that may be used to control how tty functions.

-l Display the terminal device path if the device is on an active synchronous line. A normal line on most UNIX systems is asynchronous.
-s Inhibit displaying any information. Used to test return codes.

FURTHER DISCUSSION

Most UNIX systems allow you to access a generic terminal device path with the name /dev/tty. If this is true for your system, then you do not have to know the actual terminal device path for your terminal to perform direct input and output to your terminal.

The use of direct input and output is necessary in some cases of shell script programming. You may be redirecting the output from a while loop into a file but, within the while loop, want to redirect the output of a command to your terminal. Refer to Module 108 on Programming Constructs for further information.

DIAGNOSTICS AND BUGS

not a tty Displayed if the standard input is not a terminal and -s was not specified. The tty command was called from a shell that is executing in background.
not on an active synchronous line
Displayed if -l is specified and you are not connected via an active synchronous line.

RELATED COMMANDS

The tty command writes to the standard output.

RETURN CODE

The tty command returns the following return codes upon termination. They can be used to check what happened when tty executed. Return codes are usually used in shell scripts in conjunction with conditional if statements and the test command.

0 If standard input is a terminal, completed ok.
1 The tty command failed, various reasons.
2 An invalid option was given.

APPLICATIONS

The tty command can be used to display what device your terminal is connected to on the UNIX system. It is often used by the system administrator to double check which tty port you are connected to. If maintenance requires moving your terminal cable, the port must be known in most situations. @H1 = TYPICAL OPERATION

In this activity you use the tty command to find your tty path. Begin at your shell prompt.

1.  Type tty and press Return. Notice a device path is returned; all UNIX device drivers reside in the /dev directory.
   cj> tty
   /dev/tty11
2.  Type tty -s and press Return. The tty command did not return any output, but by typing echo $? and pressing Return you can display the return code of tty. The 0 (zero) reveals tty completed without errors.
  cj> tty -s
  cj> echo $?
  0

C Shell
cj> tty -s
cj> echo $status
O

3.  Type the following code to see how you might use the tty command in a shell script.
  cj> if tty -s
  then echo "This IS a tty!"
  else echo "Sorry, you must be on a tty to run this script!"
     exit 1
  fi

C Shell
cj> tty -s
cj> if ( $status == 0 ) then
echo "This IS a tty\!"
else echo "Sorry, you must be on a tty to run this script!"
endif

4.  Turn to Module 66 (SV), Module 159 (BSD) to continue the learning sequence.


Previous Table of Contents Next