【发布时间】:2021-07-29 22:49:02
【问题描述】:
我写了一个c程序如下:
#include <stdio.h>
#include <stdlib.h>
int main() {
int a;
scanf("%d",&a);
system("/bin/sh");
return 0;
}
编译它,当我执行 a.out 文件时,它显示如下:
user@system:~/dir$ ./a.out
123 <- this is the input that I gave
$ whoami
user
所以在这里我得到了一个通过 c 调用的适当子 shell
现在,如果我通过管道使用输入执行 a.out 文件,它会显示如下:
user@system:~/dir$ echo 123 | ./a.out
user@system:~/dir$
它只是简单地执行命令,创建 shell,终止 shell 并退出。
我确信它会执行子 shell,因为在执行 echo 123 | ltrace ./a.out 时它显示如下:
system("/bin/sh" <no return ...>
--- SIGCHLD (Child exited) ---
<... system resumed> ) = 0
+++ exited (status 0) +++
这意味着创建了一个子shell
我不明白这两种方法有什么区别
【问题讨论】:
-
你期待什么?
标签: c shell terminal pipe system