1751809 Members
4821 Online
108781 Solutions
New Discussion юеВ

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 12
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.