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 Stream | Purpose | Abbreviation | File Descriptor |
|---|---|---|---|
| Standard Input | Data or text input to program | STDIN or stdin | 0 |
| Standard Output | Data or text output from program | STDOUT or stdout | 1 |
| Standard Error | Errors or other messages of importance from program | STDERR or stderr | 2 |
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
- 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"
- [[ -t 0 ]] && echo "interactive " || echo "not interactive"
Was this topic helpful?
Document Information
Modified date:
17 June 2018
UID
isg3T1025882