Check input device is a terminal: Difference between revisions

From Rosetta Code
Content added Content deleted
(Created page with "{{draft task}} In this task, the job is to check whatever input filehandle comes from terminal and mention whatever input comes from it or not. =={{header|Perl}}== <lang perl...")
 
(→‎{{header|Python}}: Python's example)
Line 24: Line 24:
Input comes from tty.
Input comes from tty.
$ true | perl6 istty.p6
$ true | perl6 istty.p6
Input doesn't come from tty.

=={{header|Python}}==
<lang python>from sys import stdin
if stdin.isatty():
print("Input comes from tty.")
else:
print("Input doesn't come from tty.")</lang>

$ python istty.pl
Input comes from tty.
$ true | python istty.pl
Input doesn't come from tty.
Input doesn't come from tty.

Revision as of 18:17, 24 September 2012

Check input device is a terminal is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

In this task, the job is to check whatever input filehandle comes from terminal and mention whatever input comes from it or not.

Perl

<lang perl>use strict; use warnings; use 5.010; if (-t) {

   say "Input comes from tty.";

} else {

   say "Input doesn't come from tty.";

}</lang>

$ perl istty.pl
Input comes from tty.
$ true | perl istty.pl
Input doesn't come from tty.

Perl 6

<lang perl6>say $*IN.t ?? "Input comes from tty." !! "Input doesn't come from tty.";</lang>

$ perl6 istty.p6
Input comes from tty.
$ true | perl6 istty.p6
Input doesn't come from tty.

Python

<lang python>from sys import stdin if stdin.isatty():

   print("Input comes from tty.")

else:

   print("Input doesn't come from tty.")</lang>
$ python istty.pl
Input comes from tty.
$ true | python istty.pl
Input doesn't come from tty.