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脚本呢?如果你知道如何让用户成为超级用户,那也不错。谢谢!
5 个解决方案
#1
48
Use the system
function.
使用该系统的功能。
system("myfile.sh"); // myfile.sh should be chmod +x
#2
15
#include <stdio.h>
#include <stdlib.h>
// ....
system("my_bash_script.sh");
#3
9
The only standard mandated implementation dependent way is to use the system()
function from stdlib.h
.
唯一的标准强制实现依赖的方法是使用stdlib.h中的system()函数。
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 sudo
but there are a few things you need to know before you can go off using sudo
.
您希望脚本以超级用户的身份运行,还是希望提高C可执行文件的权限?前者可以用sudo完成,但是在使用sudo之前,您需要知道一些事情。
#4
4
*: How to execute a command and get output of command within C++?
*:如何在c++中执行命令并获得命令的输出?
*: (Using fork,pipe,select): ...nobody does things the hard way any more...
*:(使用叉子,管道,选择):……没有人再用艰难的方式做事了……
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***.)
sudo。su. chmod 04500。(setuid()和seteuid()),但是它们要求您已经是root用户。E . g。chmod 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 /dev devices (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用户。另一方面,当加载/卸载内核模块时,这就不能很好地工作了。
#5
0
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.c
source file. Here is an example of code:
由于这是一个相当老的问题,而且这个方法还没有添加(除了system()调用函数之外),我想应该包括使用C二进制本身创建shell脚本。shell代码将驻留在文件中。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 SHELLSCRIPT
using the system()
function in main()
.
基本上,简而言之(一语双关),我们定义脚本的名称,充实脚本,将它们包含在双引号(同时插入适当的逃脱忽略shell代码中的双引号),然后调用该脚本的名称,在本例中是SHELLSCRIPT使用该系统在主要()()函数。
#1
48
Use the system
function.
使用该系统的功能。
system("myfile.sh"); // myfile.sh should be chmod +x
#2
15
#include <stdio.h>
#include <stdlib.h>
// ....
system("my_bash_script.sh");
#3
9
The only standard mandated implementation dependent way is to use the system()
function from stdlib.h
.
唯一的标准强制实现依赖的方法是使用stdlib.h中的system()函数。
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 sudo
but there are a few things you need to know before you can go off using sudo
.
您希望脚本以超级用户的身份运行,还是希望提高C可执行文件的权限?前者可以用sudo完成,但是在使用sudo之前,您需要知道一些事情。
#4
4
*: How to execute a command and get output of command within C++?
*:如何在c++中执行命令并获得命令的输出?
*: (Using fork,pipe,select): ...nobody does things the hard way any more...
*:(使用叉子,管道,选择):……没有人再用艰难的方式做事了……
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***.)
sudo。su. chmod 04500。(setuid()和seteuid()),但是它们要求您已经是root用户。E . g。chmod 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 /dev devices (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用户。另一方面,当加载/卸载内核模块时,这就不能很好地工作了。
#5
0
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.c
source file. Here is an example of code:
由于这是一个相当老的问题,而且这个方法还没有添加(除了system()调用函数之外),我想应该包括使用C二进制本身创建shell脚本。shell代码将驻留在文件中。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 SHELLSCRIPT
using the system()
function in main()
.
基本上,简而言之(一语双关),我们定义脚本的名称,充实脚本,将它们包含在双引号(同时插入适当的逃脱忽略shell代码中的双引号),然后调用该脚本的名称,在本例中是SHELLSCRIPT使用该系统在主要()()函数。