您的位置:首页 > 运维架构 > Shell

linux-c++调用shell

2016-04-25 16:12 465 查看
只要用system()就好.

如shell文件 first.sh

此shell程序的功能是把含有字符串“qiang”的文件都输出来。

#!/bin/bash
# this file looks through all the files in the current directory for the string "qiang", and then prints the names of those files to the stand ouput
for file in *
do
if grep -q qiang $file
then
echo $file
fi
done
exit 0


c++文件 mycall.cpp

#include<iostream>
#include<unistd.h>
using namespace std;
int main()
{
//    system("./a.out");
system("sh first.sh");
}


编译:

g++ mycall.cpp -o exe

执行

./exe

显示效果:

first

first.sh

hello.cpp

hello.cpp~

hzq.txt

mycall.cpp

mycall.cpp~

testdir.cpp

testdir.cpp~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: