<?xml version="1.0" encoding="windows-1252"?>
<node id="472070" title="Re: Detecting interactive/non-interactive shell (Perl Cookbook)" created="2005-07-03 16:38:36" updated="2005-07-15 06:02:16">
<type id="11">
note</type>
<author id="14909">
ybiC</author>
<data>
<field name="doctext">
As per brother [mifflin] above, from the [http://www.oreilly.com/catalog/perlckbk2/|Perl Cookbook] &lt;sup&gt;&lt;small&gt;1st ed, pp 518, 519&lt;/small&gt;&lt;/sup&gt;...
&lt;br /&gt;
 &amp;nbsp; hth,&lt;br /&gt;
 &amp;nbsp; ybiC&lt;br /&gt;
&lt;br /&gt;

&lt;p&gt;
Use &lt;tt&gt;-t&lt;/tt&gt; to test &lt;tt&gt;STDIN&lt;/tt&gt; and &lt;tt&gt;STDOUT&lt;/tt&gt;:
&lt;code&gt;sub I_am_interactive {
  return -t STDIN &amp;&amp; -t STDOUT;
}&lt;/code&gt;
&lt;/p&gt;

&lt;p&gt;
If you're on a POSIX system, test process groups:
&lt;code&gt;
sub I_am_interactive {
  local *TTY; # local file handle
  open(TTY, "/dev/tty") or die "can't open /dev/tty: $!";
  my $tpgrp = tcgetpgrp(fileno(TTY));
  my $pgrp = getpgrp();
  close TTY;
  return ($tpgrp == $pgrp);
}
&lt;/code&gt;
&lt;/p&gt;
</field>
<field name="root_node">
472045</field>
<field name="parent_node">
472045</field>
</data>
</node>
