cancel
Showing results for 
Search instead for 
Did you mean: 

Terminal or not Terminal

SOLVED
Go to solution
Marty Metras
Super Advisor

Terminal or not Terminal

Some where I saw a command/script the would tell if a script was runing on a terminal or as a process.
I have a couple scripts that can be run online or as a batch There are some interactive things that should be entered when interactive and default when batched. I know there is a command that will tell me this but I can not remember what it is.

something like
if [ "$THIS_IS_A_TERMINAL" = "YES" ]
then
echo "do something"
else
echo "let the computer do something else"
fi

Can you help?
Marty
The only thing that always remain the same are the changes.
12 REPLIES
Mark Grant
Honored Contributor

Re: Terminal or not Terminal

how about

RESULT=`tty`
if [ $RESULT = 0 ]
then
echo "do something"
else
do something that isn't an echo becuase we haven't go a terminal to write to :)
fi
Never preceed any demonstration with anything more predictive than "watch this"
Massimo Bianchi
Honored Contributor

Re: Terminal or not Terminal

if tty -s
then
echo "INTERACTIVE SESSION"
fi

Massimo

Mark Grant
Honored Contributor

Re: Terminal or not Terminal

Ok Massimo, you win this round :)
Never preceed any demonstration with anything more predictive than "watch this"
Elmar P. Kolkman
Honored Contributor

Re: Terminal or not Terminal

tty is the way to go, but redirect stderr to the same output. tty will give you the device it is running on.

RESULT=`tty 2>&1 | grep '^/dev/'`
if [ -n "$RESULT" ]
then
# You have a terminal
else
# No terminal, run unattended
fi

You could also remove the RESULT var:
if tty 2>&1 | grep -q '^/dev/'
then
# Run attended
else
# Run unattended
fi
Every problem has at least one solution. Only some solutions are harder to find.
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Terminal or not Terminal

It's even easier:

if [[ -t 0 && -t 1 && -t 2 ]]
then
echo "Stdin (0), Stdout (1), and Stderr (2) are tty devices"
fi

Generally, the test is only done for stdin.
If it ain't broke, I can fix that.
Marty Metras
Super Advisor

Re: Terminal or not Terminal

I'm not sure on this one.

if [[ -t 0 && -t 1 && -t 2 ]]
then
echo "Stdin (0), Stdout (1), and Stderr (2) are tty devices"
fi

Isn't Stdin (0), Stdout (1), and Stderr (2)always available?
It works!
Marty
The only thing that always remain the same are the changes.
Mark Grant
Honored Contributor

Re: Terminal or not Terminal

Marty,

Give up and give A.Clay the bunny :)

Anything with the standard input closed, is, by definition, not realy attached to a terminal and therefore the test is perfectly valid and smarter than the other suggestions (especially mine).
Never preceed any demonstration with anything more predictive than "watch this"
Massimo Bianchi
Honored Contributor

Re: Terminal or not Terminal

Just for info: "tty -s"

The -s option
inhibits printing of the terminal path name and any diagnostics,
providing a means to test only the exit code.



NO POINTS PLEASE.

Massimo
Marty Metras
Super Advisor

Re: Terminal or not Terminal

Done deal.
I already have the scrapt installed in many places. Tested and works great.
I like the simple things.

Thank you and happy holidays.
Marty
The only thing that always remain the same are the changes.
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