gpt4 book ai didi

shell - 我们如何通过使用 isatty 函数来区分我们程序的输入是定向的还是只是用户的输入?

转载 作者:行者123 更新时间:2023-12-01 02:18:47 24 4
gpt4 key购买 nike

我在这个链接上的 Stack Overflow 上问了一个类似的问题:

Why is it that we can redirect the input of 'less' command, but we can't run less without any arguments?

这导致我:
我们如何区分我们程序的输入是定向的还是只是用户的输入?!
有人能给我一个关于如何使用 isatty 的小例子吗?功能 ?

最佳答案

isatty 的示例:

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

int main()
{
if( isatty(STDIN_FILENO) )
puts("Connected to a terminal");
else
puts("Not connected to a terminal");

return 0;
}

正在使用:

$ gcc isatty.c 
$ ./a.out
Connected to a terminal
$ echo hello | ./a.out
Not connected to a terminal

没有比这更简单的了!

关于shell - 我们如何通过使用 isatty 函数来区分我们程序的输入是定向的还是只是用户的输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22213351/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com