【发布时间】:2016-03-20 13:01:31
【问题描述】:
有没有办法在同一个 shell 实例中执行 shell 命令?因为,system() 在命令执行后离开了启动的 shell。
【问题讨论】:
标签: c linux bash shell terminal
有没有办法在同一个 shell 实例中执行 shell 命令?因为,system() 在命令执行后离开了启动的 shell。
【问题讨论】:
标签: c linux bash shell terminal
您始终可以使用分号将您的 shell 命令构造为单行命令。如:
cd /home/user;mkdir tmp;ls
【讨论】:
您的意思是在运行您的程序的同一终端中执行命令吗?您可以通过popen 实现这一目标:
#include <stdio.h>
int main() {
FILE *f = popen("ls", "r");
char line[1024];
size_t len;
while (fgets(line, 1024, f) != NULL) {
printf("%s", line);
}
pclose(f);
return 0;
}
【讨论】:
cd 这样不行