我如何从bash的C程序提供输入? [英] How do I provide input to a C program from bash?
问题描述
我一直在读一吨左右,的bash脚本和程序测试,但我仍然无法让这个code的工作。
基本上它是一个简单的程序,要求用户进行任何的北
东
南
或西
。我启动该程序,然后立即要求输入。我只是不能得到bash脚本给它任何输入。我试着用回声
和期望
。
任何帮助是AP preciated。
下面是用于获取玩家输入的功能:
INT process_input(地图*游戏)
{
的printf(\\ n>中); 焦炭CH =的getchar();
所以char a = getchar函数(); //吃进 INT伤害=兰特()%4; 开关(CH){ 情况1:
的printf(你吸\\ n);
返回0;
打破;
案例'N':
游戏 - > proto.move(游戏中,北);
打破;
案件的:
游戏 - > _(移动)(比赛中,南);
打破;
案例E:
游戏 - > _(移动)(游戏,EAST);
打破;
案W:
游戏 - > _(移动)(游戏中,西);
打破;
案一:
游戏 - > _(攻击)(游戏,伤害);
打破;
案例'L':
的printf(你可以走:\\ n);
如果(游戏 - >&位置 - GT;北)的printf(北\\ n);
如果(游戏 - >&位置 - GT;南)的printf(SOUTH \\ n);
如果(游戏 - >&位置 - GT;东)的printf(EAST \\ n);
如果(游戏 - >&位置 - GT;西)的printf(WEST \\ n);
打破;
默认:
的printf(下一个是什么?,CH);
}
返回1;
}
这里是企图在bash脚本:
#!/斌/庆典
/桌面/ C
./ex17回声'W'
您可以从bash中使用任何这些机制的饲料投入到一个程序:
有关一个简单的线串,可以使用此处的字符串
./ EX17<<<'W'
有关多行,可以使用这里的文档
./ EX17<<'EOF'
W¯¯
输入第二行
更多的输入
EOF
或者你可以搬完了剧本,并成为一个单独的文件:
./ EX17<文件名
更一般地,你可以运行生成作为其输出所需的输入到你的程序中的指令,并将它们与管道连接在一起。例如,上述的组合也可以写成:
猫文件名| ./ex17
这是更普遍的,因为你可以替换猫
与任何类型的程序,它可以做各种计算,以确定它输出,而不是仅仅倾销的内容静态文件。
但你不能轻易地从bash中做的是推动和输入输出阅读并做出送什么作为下一个输入的决定。对于这一点,你应该看看期待。一个expect脚本会是这个样子:
#!的/ usr / bin中/预期
产卵./ex17
预计>中
发送W \\ N
期望下一个是什么?
发送这里的下一行\\ n
#打开它交给交互式用户
相互作用
I have been reading a ton about bash scripts and program testing but I am still unable to make this code work.
Basically it is a simple program that asks the user for either of north
east
south
or 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 echo
and expect
.
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:
#!/bin/bash
/Desktop/c
./ex17 echo 'w'
You can feed input into a program from bash using any of these mechanisms:
For a simple one-line string, you can use a 'here-string':
./ex17 <<<'w'
For multiple lines, you can use a "here-document":
./ex17 <<'EOF'
w
second line of input
more input
EOF
Or you can move those 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
That's more general because you can replace cat
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 file.
But what you can't easily do from bash is drive input and 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:
#!/usr/bin/expect
spawn ./ex17
expect ">"
send "w\n"
expect "Whats next?"
send "next line here\n"
# turn it back over to interactive user
interact
这篇关于我如何从bash的C程序提供输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!