检查UNIX命令行参数,管道和来自C程序的重定向 [英] Check for UNIX command line arguments, pipes and redirects from a C program

查看:206
本文介绍了检查UNIX命令行参数,管道和来自C程序的重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些问题,找出如何维护管道和重定向功能的shell一旦我发现有缺少命令行参数。

I have some problem to figure out how I can maintain the pipe and redirect functionality of a shell once I find out that there are missing command line arguments.

如果我例如使用scanf调用,将工作与重定向或管道从shell,但在没有这个我得到一个提示,我不想要。

If I for example use a scanf call, that will work with a re-direct or a pipe from a shell, but in absence of this I get a prompt, which I don't want.

我想通过argv []接受命令行参数,一个管道或重定向,但我不知道如何做,没有得到提示。如果我例如尝试这样:

I would like to accept command line arguments through argv[], a pipe or re-direct but I can't figure out how to do it with out getting the prompt. If I for example try something like this:

if(argc < 2)
    exit(0);

然后程序将终止,如果我尝试这样:

Then the program will terminate if I try this:

echo arg | myProgram

或:

myProgram < fileWithArgument



我试图查找这个,但我总是得到一些bash脚本引用。

I have tried to look this up but I always get some bash scripting reference.

推荐答案

处理这种情况的常见方法是检查标准输入流是否连接到终端,使用 isatty 或类似功能,具体取决于您的操作系统。如果是,你从命令行参数,如果没有(它被重定向),你读取标准输入。

The common way to handle situations like this is to check if the standard input stream is connected to a terminal or not, using isatty or similar functions depending on your OS. If it is, you take parameters from the command line, if not (it's been redirected), you read standard input.

这篇关于检查UNIX命令行参数,管道和来自C程序的重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆