创建C ++程序以运行shell脚本

时间:2012-02-14 02:39:03

标签: c++ shell

我正在尝试编写一个带有命令行参数值的程序,并将其传递给shell脚本。

我已经创建了shell脚本:“findname.sh”

这就是我已经为main.cpp写的

#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;
char * input1;
char my_command[50];

int main(int argc, char *argv[])
{
    input1=argv[1];
    char* command_line;
    sprintf(my_command, "./findname.sh %s", input1);
    if(argc != 2){
       system("findname.sh");
    cout << "usage: "<< argv[0] << " Some Value\n";
    exit(0);
}
command_line=argv[1];
cout << "You entered  :" << command_line <<" from the command line." << endl;

但是当我试图符合findName xxxxxx时 它说命令未找到

2 个答案:

答案 0 :(得分:3)

本着帮助你的精神,你可能想要改变:

system("findname.sh");

system(my_command);

但是你的代码很乱,无论如何都不会编译。如果它运行的话,这显然不是您运行的代码。

摆脱语法错误,以及所有不必要的东西,你将达到三行左右,这是有意义的。

答案 1 :(得分:1)

应该是:

system("./findname.sh");

而不是

system("findname.sh");