C++中嵌套调用shell script
1.准备好需要使用的shell script
1.1 这是压缩文件的shell script
mygzip.sh
#!/bin/bash
gzip /home/XXX/locn.rf
1.2 这是将本地文件传输到另一台机器上的shell script
myscp.sh
#! /usr/bin/expect -f
spawn scp -r /home/XXX/locn.rf.gz
test@10.115.XXX.XX:/home/XXX/
expect "*Password:"
send "password\r"
expect eof
2.C++中调用脚本
C++程序直接包含头文件stdlib.h,就可以使用system函数调用shell script了,非常方便。具体system的说明可以查询
$ man system
myshell.cpp
#include <stdlib.h>
#include <string>
int main()
{
std::string res;
res = system("./mygzip.sh");
res = system("./myscp.sh");
if (res == 0){
return 0;
}
else {
return 1;
}
}
3.编译运行
$ g++ myshell.cpp -o test
$ ./test
实现了将文件先压缩,然后传输到对应的目标地址。