Linux: Feststellen ob stdout umgeleitet wird oder nicht?

Persönliche Seite #1500243
Lesenswert?

"isatty() is a function that returns TRUE if the handle is associated 
with a TTY (e.g. keyboard, screen, printer, serial port), and FALSE in 
the other circumstances (e.g. the handle is associated with a regular 
file, or is a part of a pipeline). For example, if the standard output 
is redirected to a file, or piped to another program, then 
isatty(STDOUT) returns FALSE."

http://www.cs.toronto.edu/~ramona/cosmin//TA/prog/sysconf/
http://linux.die.net/man/3/isatty
Gast #1500320
Lesenswert?

Danke.

Falls jemand nachvollziehen möchte.
1
#include <stdio.h>
2
#include <unistd.h>
3

4
int main(void) {
5
    FILE * ferror = fdopen(2, "w");
6
    if(isatty(1)) {
7
  fprintf(ferror, "ja\n");
8
  printf("ja\n");
9
    }
10
    else {
11
  fprintf(ferror,"nein\n");
12
  printf("nein\n");
13
    }
14
    return 0;
15
}
1
matrix:/pool/linux/output# gcc -o main main.c -Wall
2
matrix:/pool/linux/output# ./main
3
ja
4
ja
5
matrix:/pool/linux/output# ./main > x
6
nein
7
matrix:/pool/linux/output# cat x
8
nein
9
matrix:/pool/linux/output#

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren