C语言 如何从 bash 向 C 程序提供输入?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16508817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:18:53  来源:igfitidea点击:

How do I provide input to a C program from bash?

cbash

提问by blankwall

I have been reading a ton about bash scripts and program testing but I am still unable to make this code work.

我一直在阅读大量关于 bash 脚本和程序测试的内容,但我仍然无法使这段代码工作。

Basically it is a simple program that asks the user for either of northeastsouthor west. I start the program then it immediately asks for input. I just can not get the bash script to give it any input. I tried using echoand expect.

基本上它是一个简单的程序,要求用户输入northeastsouthwest。我启动程序然后它立即要求输入。我只是无法让 bash 脚本给它任何输入。我尝试使用echoexpect

Any help is appreciated.

任何帮助表示赞赏。

Here is the function used to get the players input:

这是用于获取玩家输入的函数:

int process_input(Map *game)
{
    printf("\n> ");

    char ch = getchar();
    char a = getchar(); //eat enter

    int damage = rand() % 4;

    switch(ch) {

        case -1:
            printf("you suck\n");
            return 0;
            break;
        case 'n':
            game->proto.move(game, NORTH);
            break;
        case 's':
            game->_(move)(game, SOUTH);
            break;
        case 'e':
            game->_(move)(game, EAST);
            break;
        case 'w':
            game->_(move)(game, WEST);
            break;
        case 'a':
            game->_(attack)(game, damage);
            break;
        case 'l':
                    printf("You can go:\n");
                    if(game->location->north) printf("NORTH\n");
                    if(game->location->south) printf("SOUTH\n");
                    if(game->location->east) printf("EAST\n");
                if(game->location->west) printf("WEST\n");
                    break;
        default:
            printf("Whats next?", ch);
        }
        return 1;
}

And here is the attempt at a bash script:

这是对 bash 脚本的尝试:

   #!/bin/bash
    /Desktop/c
    ./ex17 echo 'w'

回答by Mark Reed

You can feed input into a program from bash using any of the following mechanisms.

您可以使用以下任何一种机制将输入从 bash 输入到程序中。

For a single line of input, you can use a here-string:

对于单行输入,您可以使用here-string

./ex17 <<<'w'

For multiple lines, you can use a here-document:

对于多行,您可以使用here-document

./ex17 <<'EOF'
w
second line of input
more input
EOF

Or you can move those lines out of the script and into a separate file:

或者您可以将这些行移出脚本并放入一个单独的文件中:

./ex17 <filename    

More generally, you can run a command that generates as its output the desired input to your program, and connect them together with a pipe. For instance, the above could also be written:

更一般地说,您可以运行一个命令,该命令生成程序所需的输入作为其输出,并用管道将它们连接在一起。比如上面的也可以写成:

cat filename | ./ex17

or the original example as

或原始示例为

echo w | ./ex17

That's more general because you can replace catand echohere with any sort of program, which can do all sorts of computation to determine what it outputs instead of just dumping the contents of a static string or file.

这更通用,因为您可以用任何类型的程序替换catand echohere ,这些程序可以进行各种计算以确定它输出的内容,而不仅仅是转储静态字符串或文件的内容。

But what you can't easily do from bash is drive input, read output, and make decisions about what to send as the next input. For that, you should look at expect. An expect script would look something like this:

但是,您无法从 bash 轻松完成的是驱动输入、读取输出以及决定将什么作为下一个输入发送。为此,您应该查看expect。一个期望脚本看起来像这样:

#!/usr/bin/env expect
spawn ./ex17
expect ">"
send "w\n"
expect "Whats next?"
send "next line here\n"
# turn it back over to interactive user
interact

回答by Ahmed Masud

Try this: first:

试试这个:首先:

 echo w | ./ex17 

This will send w to the example and output the move. This is called piping; and it essentially connects the stdout of echo to the stdin of ex17

这会将 w 发送到示例并输出移动。这称为管道;它本质上将 echo 的标准输出连接到 ex17 的标准输入

回答by Arrigo Pierotti

You can do either a Ccode or a Bashscript, not a mixed one. Use scanf()for reading from keyboard (it stops reading when you hit ENTER) and complete this code in Clanguage.

您可以执行C代码或Bash脚本,而不是混合使用。使用scanf()从键盘读取(它停止阅读,当你敲击回车键),并在完成这个代码C的语言。