加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

shell – 我们如何通过使用isatty函数来区分对程序的输入是定向

发布时间:2020-12-15 21:00:03 所属栏目:安全 来源:网络整理
导读:我在此链接上询问了有关Stack Overflow的类似问题: Why is it that we can redirect the input of ‘less’ command,but we can’t run less without any arguments? 这让我想到了这个: 我们如何区分我们程序的输入是针对的,还是仅仅是用户的输入? 谁能给
我在此链接上询问了有关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

没有那么简单!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读