Operating System - HP-UX
1752780 Members
6345 Online
108789 Solutions
New Discussion юеВ

Re: Terminal or not Terminal

 
SOLVED
Go to solution
A. Clay Stephenson
Acclaimed Contributor

Re: Terminal or not Terminal

The -t test does more than simply look to see if the file descriptor is open, it tests to see whether or not, it is assiciated with a tty (terminal) device. It can be used, for example, to test whether a process is interactive or if stdin is coming from a pipe.

Try this trivial test:

Create my.sh,
if [[ -t 0 ]]
then
echo "Terminal"
else
echo "Not Terminal"
fi

Now invoke it thusly:
my.sh

and then
echo "Dummy" | my.sh


Man test for details. You could also use the more familiar "if [ -t 0 ]" syntax but the double brackets are more efficient because it uses the POSIX (or Korn) shell's internal test rather than forking the external test command.

If it ain't broke, I can fix that.
Marty Metras
Super Advisor

Re: Terminal or not Terminal

Ah! I see.
So when I pipe to something, the something is not part on the part of the current process it is a subprocess that is not part of the terminal process. Well kinda.
Thanks for the man test. Didn't know tat was there.

Marty
The only thing that always remain the same are the changes.
Michael Schulte zur Sur
Honored Contributor

Re: Terminal or not Terminal

Hi Marty,

stdin,stdout,stderr are not allways associated with a device. Not only when you pipe, also when you redirect <> or run a process in the backgound, lets say as a crontab or at job.

have fun,

Michael