gpt4 book ai didi

linux - Unix 中的管道。实现示例时出错

转载 作者:太空宇宙 更新时间:2023-11-04 10:09:58 24 4
gpt4 key购买 nike

我是 Unix 和 Linux 的新手。我正在尝试 Adam Hoover,Pg 159-162 一书中的“使用 C 和 Unix 进行系统编程”一书中的示例。我正在使用 Linux Ubuntu。

我想通过管道传输两个 C 代码(夏季 | bingo)。

夏天.c

#include<stdio.h>
void main()
{
int x,s;
s =0 ;
while(1)
{
printf("#?");
scanf("%d",&x);
if (x == 0)
break;
s = s +x;
printf("sum = %d\n",s);
}
}

宾果游戏

#include<stdio.h>
#include<string.h>
void main()
{
char s[80];
while(1)
{
scanf("%s",s);
if ( strcmp(s,"sum=5") == 0 )
{
printf("Bingo!\n");
}
else if( strcmp(s,"sum=12") == 0)
{
break;
}
}
}

在 Linux 终端中,我遵循命令。

  1. gcc -o summer summer.c

  2. gcc -o bingo bingo.c

  3. 夏天 |宾果

我做不到,它给出了一个错误。

“当前未安装程序‘summer’。

您可以通过键入以下内容来安装它:

sudo apt install chiark-utils-bin

没有找到命令 'bingo',您的意思是:

从包“bing”(宇宙)中命令“bing”

来自包“bino”(宇宙)的命令“bino”

宾果游戏:找不到命令“

summer 和 bingo 文件正在使用 ./summer./bingo

但是,./summer|宾果游戏不起作用。

我曾尝试使用“<”标准输入或“>”标准输出。这些都很完美。 但是“|”给出了一个错误

任何帮助都将对学习 UNIX 非常有用。

最佳答案

选项 1:显式指定两个命令的路径

./summer | ./bingo

选项 2:将当前路径添加到 PATH 以便找到它们。在 bash 上,您可以执行

export PATH="$PATH:$(pwd)"
summer | bingo

注意加“.”添加到 PATH 并不是最安全的做法,而是实际指定包含 summerbingo 的路径,或者使用 pwd 添加当前文件夹明确地。

关于linux - Unix 中的管道。实现示例时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49721842/

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