tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: interactive shell detection in shrc



    Date:        Fri, 4 Oct 2024 07:53:59 -0700
    From:        George Georgalis <george%galis.org@localhost>
    Message-ID:  <CAHK3FNzAEf5+hZ9Mix_46rermOXBiDj5qN4RzcwF7W=ipWxVZw%mail.gmail.com@localhost>


  | > test -t 0
  | >
  | > is reliable.

It is, for what it is suppposed to do.

  | Is there a case where this is not the solution?

To which problem exactly?   If to determine whether stdin
(in that case) is a tty, then no, that always works.

But if the idea is to use just that to determine if a shell
is interactive, as in:

  | When I decided this is what interactive means
  | all of the edge cases went away. Is there a
  | case I am not considering?

what that might be intending to mean, then no, that won't
work at all.

Run any script (from any interactive shell, as a command line) as

	sh script
		(or just ./script if it has 'x' perms & perhaps a #!)

and in the script if you do "test -t 0" you'll get "true" - in that
script, standard input is a terminal, the same one as the shell from
which the script was run from.   Yet the shell running the script is
certainly not interactive.   To be interactive, then as well as stderr
also needing to be a terminal, the shell needs to be reading its commands
from that standard input (being a terminal).   There's no shell operator
or function to tell you which file descriptor the shell is reading its
command input from (none of the script's business.)

And while here, answering another question I saw in another (I think
later) message on this topic, if "test -t N" is what you need to use,
go ahead and use it, the "-t" operator has been in the test command
since the test command was first released (7th edn Bell Labs unix).

There will be zero portability problems with that usage.

Originally the "N" (in test -t N) was optional, defaulting to 0,
so just

	test -t

was equivalent to "test -t 0" - but that's completely bogus, and no
longer expected to work anywhere.

test with a single operand is supposed to simply test if the operand
is the null string (false) or not (true).

	test "$x"

is true, if $x is not the empty string.   But consider if x=-t ...

The original test command was a real mess.   Used only as currently
defined it is just fine.

kre



Home | Main Index | Thread Index | Old Index