0

I have never used c++ before and what I have so far is:

#include <bits/stdc++.h>

#include <string>

using namespace std;



int main (int argc, char* argv[]) {
  string command="./findName.sh";

  if(argc == 2){
    system((command + " " + argv[1]).c_str());
  }
}

This program just takes in one parameter and passes it to the script but I want it to pass more than one in the form of a string with spaces!

veksen
  • 6,863
  • 5
  • 20
  • 33
  • Well I don't have a book but I do know I need to use a for loop I just don't know how to do that – Bannanaanana Feb 21 '20 at 02:21
  • Then find an online tutorial. You seem to understand that `argc` is the number of parameters passed, and that `argv` is an array of those parameters. Write a loop that processes that array. Search the tutorial for `loops`. – Ken White Feb 21 '20 at 02:23
  • 3
    If you are serious about learning C++, then you will have to get a book. You cannot learn C++ without a good book. You won't learn anything from some lightweight Youtube videos, or someone's blog on some web site somewhere. – Sam Varshavchik Feb 21 '20 at 02:26
  • 3
    You should *never* `#include `. It is not proper C++. It ruins portability and fosters terrible habits. See [Why should I not `#include `](https://stackoverflow.com/q/31816095). – L. F. Feb 21 '20 at 03:01

2 Answers2

0

Well, this may work for any number of params even none:

int main (int argc, char* argv[]) {
       string command="./findName.sh";
        string params = "";
        for(int i = 1; i < argc; i++){
            params += " " + string(argv[i]);
        }
        if(argc > 1){
           cout << (command + params).c_str() << endl;
        }

        system((command + params).c_str());
        return 1;
    }
Rubens
  • 61
  • 1
  • 3
0

Creating a C (or C++) program for just calling a a shell script is very common when you need to implement a "setuid root" shell script.

Reason is that, for security, most Unix/Unix-like system won't allow you to directly run a "setuid root" shell script. So, creating a simple wrapper executable does the job.

#include <bits/stdc++.h>
#include <string>
using namespace std;
int main (int argc, char* argv[]) {
   string command = "./findName.sh";
   for(int i=1; i<argc;i++) { command.append(" ").append(argv[i]) };
   system(command.c_str());
}
rodrigovr
  • 454
  • 2
  • 7