isatty

isatty - test whether a file descriptor refers to a terminal

#include <stdio.h>
#include <unistd.h>

int isatty(int fd);

int main(int argc, char *argv[]) {
  printf("isatty : %d 
", isatty(STDIN_FILENO));
  return 0;
}
gcc test.c

a.out 的 stdin 來自 terminal,所以 isatty(STDIN_FILENO) 為 1

$ ./a.out
isatty : 1

a.out 的 stdin 是來自 pipe,不是來自 terminal,所以 isatty(STDIN_FILENO) 為 0

$ echo "123" | ./a.out
isatty : 0
原文地址:https://www.cnblogs.com/youchihwang/p/10563328.html