user1208154
user1208154

Reputation: 1

Creating C++ program to run shell script

I'm trying to write a program that takes a command line argument value and pass it to a shell script.

I already created the shell script: " findname.sh "

and this is what I have already written for my 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;

But when I try to rune findName xxxxxx it says command not found

Upvotes: 0

Views: 6429

Answers (2)

dead_OTMOPO3
dead_OTMOPO3

Reputation: 83

It should be:

system("./findname.sh");

instead of

system("findname.sh");

Upvotes: 1

Paul Beckingham
Paul Beckingham

Reputation: 14895

In the spirit of helping you, you probably want to change:

system("findname.sh");

to

system(my_command);

But your code is a mess and does not compile anyway. This is clearly not the code you ran, if it ran.

Get rid of the syntax errors, and all the unnecessary stuff, and you'll be down to three lines or so that make sense.

Upvotes: 3

Related Questions