0

Hello I am trying to write a program where I feed the C stdlib a bash system call that prints the output to a file. Currently, system() will only print to console and won't be redirected to the file.

string read_command = "dd if=" + io_filesystem + "/" + filename + " of=" + output_directory + "/" +
    "tmp.tmp bs=" + bs_string + " count=1";

string write_command =  "dd if=" + output_directory + "/" + "tmp.tmp  of=" + 
    io_filesystem + "/" + "tmp.tmp bs=" + bs_string + " count=1";

string system_read_command = "echo $(" + read_command + ") >>" + output_directory + "/out.log";
status = system(system_read_command.c_str());

string system_write_command = "echo $(" + write_command + ") >>" + output_directory + "/out.log";
status = system(system_write_command.c_str());

I would like the output of the second command to be appended to the end of the file without overwriting the first command output.

John Frye
  • 255
  • 6
  • 22
  • 1
    I really, really, really hope for your sake that those arguments are not user specified. A single space in a filename is going to cause chaos here. – tadman Nov 27 '17 at 17:30
  • Only if you are going to instantiate the class in your own executable. At that point, it's your onus to understand the how to use the methods. That really isn't related to the question however. – John Frye Nov 27 '17 at 17:39
  • It's generally a lot safer to properly shell escape things so simple mistakes don't cause frustrating errors, or so if this ends up in a product you distribute you don't open yourself to shell attacks. I only point this out because code like this, once written, is often left untouched for a long time and it becomes a hidden liability. – tadman Nov 27 '17 at 17:40
  • how do you "shell escape" things. and yes I know what your saying but is it possible to get the output to go to a file – John Frye Nov 27 '17 at 17:41
  • Depends on your shell, where this would be slightly different for Windows vs. POSIX. Typically surrounding it in `"..."` and escaping any `"` characters as `\"` does the trick. – tadman Nov 27 '17 at 17:43
  • What is *“a bash system call”*? – spectras Nov 27 '17 at 17:44
  • http://www.cplusplus.com/reference/cstdlib/system/ – John Frye Nov 27 '17 at 17:45
  • @JohnFrye> does not answer my question. That is the documentation of stdlib's `system` function. It is not a system call, and is unrelated to bash (check for yourself, “bash” isn't even mentioned on the page). Seems to me you have a deep misunderstanding of what those are, as your sample code shows. – spectras Nov 27 '17 at 17:46
  • assuming you are running a c program in a bash environment, you can invoke bash with the c stdlib system function as described by the documentation above. perhaps my nomenclature is lacking and if so I apologize – John Frye Nov 27 '17 at 17:48
  • @tadman could you provide an example of surrounding it in "..." and escaping any " characters as \" please. Unfortunately, I can't grasp what the syntax would look like from your comment – John Frye Nov 27 '17 at 17:51
  • That doesn't output to a file as is? I just ran it (with simpler commands) and the redirections to out.log works. Is output_directory set reasonably and does it point somewhere you're allowed to write? – EdmCoff Nov 27 '17 at 17:51
  • it seems to create the out.log file but does not write the output to it. could be environment issue; im on rhel 7 I believe – John Frye Nov 27 '17 at 17:52
  • 1
    Well, ISO standard merely guarantees that `system()` exists, saying nothing of what environment or language it uses. POSIX.1-2008 adds that the language must be the common shell language, as defined by Shell and Utilities volume of POSIX.1-2008. It still says nothing about bash. – spectras Nov 27 '17 at 17:54
  • 1
    May I suggest you run the external program using the tools designed for that purpose, such as `fork()`, `pipe`, `exec` and `wait`? – spectras Nov 27 '17 at 17:55

0 Answers0