Linux 如何从 C++ 程序运行 bash 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/646217/
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-08-03 17:05:55  来源:igfitidea点击:

How to run a bash script from C++ program

c++linuxbashshell

提问by Kredns

Bash scripts are very useful and can save a lot of programming time. So how do you start a bash script in a C++ program? Also if you know how to make user become the super-user that would be nice also. Thanks!

Bash 脚本非常有用,可以节省大量编程时间。那么如何在 C++ 程序中启动 bash 脚本呢?此外,如果您知道如何让用户成为超级用户,那也很好。谢谢!

采纳答案by Mehrdad Afshari

Use the systemfunction.

使用该system功能。

system("myfile.sh"); // myfile.sh should be chmod +x

回答by rlbond

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

// ....


system("my_bash_script.sh");

回答by dirkgently

The only standard mandated implementation dependent way is to use the system()function from stdlib.h.

唯一的标准强制实现依赖方式是使用system()来自stdlib.h.

Also if you know how to make user become the super-user that would be nice also.

此外,如果您知道如何让用户成为超级用户,那也很好。

Do you want the script to run as super-user or do you want to elevate the privileges of the C executable? The former can be done with sudobut there are a few things you need to know before you can go off using sudo.

您希望脚本以超级用户身份运行还是希望提升 C 可执行文件的权限?前者可以完成,sudo但在开始使用sudo.

回答by Mr.Ree

StackOverflow: How to execute a command and get output of command within C++?

StackOverflow:如何在 C++ 中执行命令并获取命令的输出?

StackOverflow: (Using fork,pipe,select): ...nobody does things the hard way any more...

StackOverflow: (Using fork,pipe,select): ...没有人再用困难的方式做事了...

Also if you know how to make user become the super-user that would be nice also. Thanks!

此外,如果您知道如何让用户成为超级用户,那也很好。谢谢!

sudo. su. chmod 04500. (setuid() & seteuid(), but they require you to already be root. E..g. chmod'ed 04***.)

须藤。苏。chmod 04500. (setuid() & seteuid(),但它们要求你已经是 root。例如 chmod'ed 04***。)

Take care. These can open "interesting"security holes...

小心。这些可以打开“有趣的”安全漏洞......

Depending on what you are doing, you may not need root. (For instance: I'll often chmod/chown /devdevices (serial ports, etc) (under sudo root) so I can use them from my software without being root. On the other hand, that doesn't work so well when loading/unloading kernel modules...)

根据您在做什么,您可能不需要 root。(例如:我会经常chmod/chown /dev设备(串行端口等)(在sudo root 下)所以我可以从我的软件中使用它们而不是 root。另一方面,当加载/卸载内核模块...)

回答by Yokai

Since this is a pretty old question, and this method hasn't been added (aside from the system()call function) I guess it would be useful to include creating the shell script with the C binary itself. The shell code will be housed inside the file.csource file. Here is an example of code:

由于这是一个非常古老的问题,并且尚未添加此方法(除了system()调用函数),我想包括使用 C 二进制文件本身创建 shell 脚本会很有用。shell 代码将保存在file.c源文件中。下面是一个代码示例:

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

#define SHELLSCRIPT "\
#/bin/bash \n\
echo -e \"\" \n\
echo -e \"This is a test shell script inside C code!!\" \n\
read -p \"press <enter> to continue\" \n\
clear\
"

int main() {

system(SHELLSCRIPT);
return 0;
}

Basically, in a nutshell (pun intended), we are defining the script name, fleshing out the script, enclosing them in double quotes (while inserting proper escapes to ignore double quotes in the shell code), and then calling that script's name, which in this example is SHELLSCRIPTusing the system()function in main().

基本上,简而言之(双关语),我们正在定义脚本名称,充实脚本,将它们括在双引号中(同时插入适当的转义符以忽略 shell 代码中的双引号),然后调用该脚本的名称,即在这个例子中SHELLSCRIPT使用system()函数 in main()