C 当通过管道输入时,系统(“bin/sh”)立即关闭

C 当通过管道输入时,系统(“bin/sh”)立即关闭,c,shell,terminal,pipe,system,C,Shell,Terminal,Pipe,System,我写了一个c程序如下: #include <stdio.h> #include <stdlib.h> int main() { int a; scanf("%d",&a); system("/bin/sh"); return 0; } 它只是执行命令,创建shell,终止shell并退出 我确信它执行子shell,因为在执行echo 123 | ltrace

我写了一个c程序如下:

#include <stdio.h>
#include <stdlib.h>

int main() {
        int a;
        scanf("%d",&a);
        system("/bin/sh");
        return 0;
}
它只是执行命令,创建shell,终止shell并退出

我确信它执行子shell,因为在执行
echo 123 | ltrace./a.out
时,它显示如下:

user@system:~/dir$ ./a.out
123                           <- this is the input that I gave
$ whoami
user
user@system:~/dir$ echo 123 | ./a.out
user@system:~/dir$
system("/bin/sh" <no return ...>
--- SIGCHLD (Child exited) ---
<... system resumed> )                                                              = 0
+++ exited (status 0) +++
系统(“/bin/sh”
---SIGCHLD(孩子退出)---
)                                                              = 0
+++已退出(状态0)+++
这意味着创建了一个子外壳

我不明白这两种方法有什么不同

sh
将读取
stdin
,直到遇到EOF。 在第一种情况下,您不发送
EOF
(这可以通过按
Ctrl-D
来完成,具体取决于您的环境)。在第二种情况下,
EOF
发生在管道输入耗尽之后(即
echo 123
完成之后)。 为了更好地理解,您可以使用一个简单的程序模拟
sh
行为:

#include <stdio.h>
int main(void)
{
    char input[100];
    while(fgets(input, 100, stdin)) {
        printf("Echoing back: %s\n", input);
    }
    return 0;
}
#包括
内部主(空)
{
字符输入[100];
而(fgets(输入,100,标准输入)){
printf(“回显:%s\n”,输入);
}
返回0;
}

您可以编译它并使用
system()
调用而不是
sh
运行,然后观察类似的行为。

您期望什么?当我运行
(echo 123;echo pwd)|/a.out
时,它不会在终端中运行命令。在将123赋值给变量之后,“pwd”仍然在输入流中。未遇到EOF,但它仍不打印目录。在原始代码中更改
系统(“/bin/sh”)
scanf
的顺序时,它工作正常。