C语言 如何从 C 执行 bash 命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29762189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:52:28  来源:igfitidea点击:

How to execute bash commands from C?

bashcgzip

提问by tarabyte

Is there a way to run command line utilities, e.g. gzip, into a C app?

有没有办法在gzipC 应用程序中运行命令行实用程序?

回答by jayhendren

Use system():

使用system()

#include <stdlib.h>
int status = system("gzip foo");

See the man page (man 3 system) for more detailed information on how to use it.

有关man 3 system如何使用它的更多详细信息,请参阅手册页 ( )。

By the way, this question already has an answer here: How do I execute external program within C code in linux with arguments?

顺便说一下,这个问题在这里已经有了答案:How do I execute external program within C code in linux in with arguments?