home | list info | list archive | date index | thread index

Re: [OCLUG-Tech] how does one test if bash shell is interactive

  • Subject: Re: [OCLUG-Tech] how does one test if bash shell is interactive
  • From: "Ian! D. Allen" <idallen [ at ] idallen [ dot ] ca>
  • Date: Thu, 11 Aug 2005 09:09:06 -0400
Adrian Irving-Beer wrote:
> Now, if you really want to know if your script is being run by a
> *user* rather than in the background (by a system script, cron, etc.),
> then you can check the result of 
> 
> 	test -t 0
> or
> 	[ -t 0 ]
> 
> to determine if standard input is a terminal.

That will give the wrong answer if input to the script ever comes from
something that isn't a terminal, e.g.

   $ script <input
   $ script <input &
   $ sort input | script
   $ sort input | script &

In all cases above, a real interactive user at a shell is calling the
script; but, standard input is not a terminal and "-t 0" fails.

-- 
-IAN!  Ian! D. Allen       Ottawa, Ontario, Canada - www.ottawa.ca
       EMail: idallen [ at ] idallen [ dot ] ca   Home Page: http://www.idallen.com/
       College professor (Linux) via: http://teaching.idallen.com/
       Support free and open public digital rights:  http://eff.org/