Recent

Author Topic: [SOLVED] Linux: How to detect whether STDOUT was redirected?  (Read 3825 times)

Hartmut

  • Hero Member
  • *****
  • Posts: 742
[SOLVED] Linux: How to detect whether STDOUT was redirected?
« on: September 28, 2018, 06:38:43 pm »
I want a function which tells me, whether STDOUT of my console program was redirected or not like
   myprogram > outfile

In http://forum.lazarus.freepascal.org/index.php/topic,41936.0.html there is a solution for Windows, but I need it for Linux (Ubuntu 18.04). Can somebody help?

I'm an absolute beginner on Linux, so please be not to short in your answer. Thanks a lot in advance.
« Last Edit: September 28, 2018, 10:13:50 pm by Hartmut »

mpv

  • Newbie
  • Posts: 6
Re: Linux: How to detect whether STDOUT was redirected?
« Reply #1 on: September 28, 2018, 09:08:53 pm »
You can check stdout is a terminal using isatty() function. If not then stdout is redirected

Hartmut

  • Hero Member
  • *****
  • Posts: 742
Re: Linux: How to detect whether STDOUT was redirected?
« Reply #2 on: September 28, 2018, 09:41:21 pm »
That sounds interesting. But I don't find this function, in which unit is it?
http://www.math.uni-leipzig.de/pool/tuts/FreePascal/units/node12.html#SECTION0012369000000000000000
says it is in unit "Linux", but I still get a compilation error: Identifier not found "IsATTY"

Thanks for your help.

alex256

  • New Member
  • *
  • Posts: 24
Re: Linux: How to detect whether STDOUT was redirected?
« Reply #3 on: September 28, 2018, 09:50:45 pm »
Code: Pascal  [Select][+][-]
  1. uses
  2.     termio;
  3.  

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Linux: How to detect whether STDOUT was redirected?
« Reply #4 on: September 28, 2018, 10:04:33 pm »
Code: Pascal  [Select][+][-]
  1. {$ifdef fpc}{$mode delphi}{$H+}{$endif}
  2. uses termio;
  3. begin
  4. writeln(IsATTY(output));
  5. end.
Specialize a type, not a var.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Linux: How to detect whether STDOUT was redirected?
« Reply #5 on: September 28, 2018, 10:05:13 pm »
Alex , posts crossed.
Specialize a type, not a var.

Hartmut

  • Hero Member
  • *****
  • Posts: 742
Re: Linux: How to detect whether STDOUT was redirected?
« Reply #6 on: September 28, 2018, 10:13:23 pm »
It works! Thanks a lot to all who helped me.

 

TinyPortal © 2005-2018