简体   繁体   English

从C程序执行Shell命令

[英]Executing shell commands from a C program

Is it possible to write to the stdin of a running shell (Bash) using a C program? 是否可以使用C程序写入正在运行的Shell(Bash)的stdin? Actually I want to execute commands using bash from my C program without using one of the exec*() system calls. 实际上,我想从我的C程序中使用bash执行命令,而不使用exec*()系统调用之一。

/bin/echo is a C program, usually. /bin/echo通常是C程序。

/bin/echo ls | sh

Voilà! 瞧! AC program just wrote to the standard input of a running shell, and a command was executed. AC程序仅写入正在运行的Shell的标准输入,并执行了命令。 This is demonstrably possible. 这是可能的。

man popen is now your friend. man popen现在是你的朋友。 (Any more is really beyond SuperUser's scope, as is discussion of other things such as system() and the need for quoting.) (还有其他任何事情超出了SuperUser的范围,对system()和引用需求等其他问题的讨论也是如此。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM