IBM Support

Errors from the stty command

Troubleshooting


Problem

In some cases if a script is running the /usr/bin/stty command it will produce errors such as: stty: tcgetattr: A specified file does not support the ioctl system call. stty: tcgetattr: Not a typewriter

Cause

This can be caused by running the stty command inside a script or in a non-interactive environment with no terminal (tty) attached.

Diagnosing The Problem

If a script or job issues an stty command, such as:
stty erase \^H (sets the erase character to the <BACKSPACE> key)
stty erase \^? (sets the erase character to the <DELETE> key)

when there is no interactive session, you may see one of the errors above.

Standard I/O Streams
In any running process there are three main file descriptors to provide input to the process and output from the process:

Data StreamPurposeAbbreviationFile Descriptor
Standard InputData or text input to programSTDIN or stdin0
Standard OutputData or text output from programSTDOUT or stdout1
Standard ErrorErrors or other messages of importance from programSTDERR or stderr2
In an interactive process such as the Korn shell (ksh), STDIN is reading from the keyboard, STDOUT is sending to the user's terminal (or pseudo-terminal for a network login), and STDERR is usually the user's terminal also.

For a non-interactive process, any of these I/O streams may be closed. STDIN won't be set if it's a ksh shell script running in a batch job, for example. This is why the stty command fails in those cases.

Typically the stty errors are seen when the command is issued within jobs run from cron or at, or run from the /etc/inittab.

Resolving The Problem

If the stty command is still necessary in the program or script, and it may be run either from the user's terminal or a batch job such as cron, your script can test for the presence of an interactive environment before running the stty. There are multiple ways to accomplish this.

1. Check for an associated tty (or pseudo-tty)
Test to see if the /usr/bin/tty command exits with a return code of 0 (true, there is a tty associated with this shell) or 1 (no tty). Usually the /usr/bin/tty command returns the name of the tty you are using, such as

    $ tty
    /dev/pts/3
Using the '-s' option to tty will prevent it from returning the name of a tty, and just come back with a 0 or 1 status. An example shell script line to use this to conditionally run the stty command only when tty returns with a 0 would look like:
    tty -s && stty erase ^H

2. Test for the existence of one of the 3 file descriptors
Usually STDIN is closed in non-interactive scripts, so we can use the Korn shell's -t test against file descriptor 0:
    if [[ -t 0 ]]; then
       echo "interactive"
    else
       echo "not interactive"
or in a single line:
    [[ -t 0 ]] && echo "interactive " || echo "not interactive"

[{"Product":{"code":"SWG10","label":"AIX"},"Business Unit":{"code":"BU058","label":"IBM Infrastructure w\/TPS"},"Component":"Not Applicable","Platform":[{"code":"PF002","label":"AIX"}],"Version":"Version Independent","Edition":"","Line of Business":{"code":"LOB08","label":"Cognitive Systems"}}]

Document Information

Modified date:
17 June 2018

UID

isg3T1025882