-5

I want to execute a script from my C++ codes that needs an argument. this argument is the context of a txt file that I need to cat to provide the argument. how can I call this script in C++ with argument ?

here is my code : my_script.sh needs an argument, I want to get the value of this argument by catting my_file.txt. my question is could cat /tmp/my_file.txt be interpreted from C++ ??

const char * my_array[] = {

"/dir/my_script.sh `cat /tmp/my_file.txt` "   
  .
  .
  .   
 };
gr8code2be
  • 47
  • 4

2 Answers2

2

The backquotes, or any other construct and interpreted by the shell (bash) when reading a line, and it then executes the command after converting the parameters. When you execute a command you start it with its parameters, or can use system, or the bash -c construct to parse the parameters, but this convertion does not occurs.

I would strongly advice you to use a shell script that takes the name of the file as parameter, and that call any command that you want using backquotes or $() because the script is execute by the shell and the shell knows what to do with backquotes and $().

You could have an intermediary script, say /path/to/laucher containing

#! /bin/bash

/dir/my_script.sh `cat $1`

and in your c++ code, you just call :

system("/path/to/launcher /tmp/my_file.txt");
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • 1
    `system()` won't be able to return the output of the executed command to the caller. I think what the OP rather needs is to kick that idea with the backticks, and use a pipe to execute the command they want. – πάντα ῥεῖ Sep 24 '14 at 15:44
0

I have never heard of back-quotes in C++ strings or char arrays to call arbitrary code.

Are you sure you do not want the system() function ?

Yamakuzure
  • 367
  • 2
  • 9
  • `system()` won't be able to return the output of the executed command to the caller. I think what the OP rather needs is to kick that idea with the backticks, and use a pipe to execute the command they want. – πάντα ῥεῖ Sep 24 '14 at 15:43
  • No, but you can route the output and check the file(s). That is what I do regularly with perl, so why not do this with C++? *BUT* the command "foo 1>foo.out 2>foo.err" isn't exactly portable... – Yamakuzure Sep 25 '14 at 08:59