Note that there are some explanatory texts on larger screens.

plurals
  1. POsh not found when executing shell command from C++
    text
    copied!<p>I have a function in my C++ program like this:</p> <pre><code>std::string SysExec::executeAndReturnResult(char* cmd) { //cout &lt;&lt; "[SHELL] : " &lt;&lt; cmd &lt;&lt; endl; FILE* pipe = popen(cmd, "r"); if (!pipe) return "ERROR"; char buffer[128]; std::string result = ""; while (!feof(pipe)) { if (fgets(buffer, 128, pipe) != NULL) result += buffer; } pclose(pipe); return result; } </code></pre> <p>When I call it </p> <pre><code>SysExec::executeAndReturnResult("/usr/bin/irsend -d /var/run/lirc/lircd-lirc0 SEND_ONCE Samsung 4"); </code></pre> <p>I get this:</p> <pre><code>sh: 4: not found </code></pre> <p>However, if I try to same command from the console, it works fine.</p> <p>I would like to mention that calling it with a non-numeric argument works with no issue — the following works fine:</p> <pre><code>SysExec::executeAndReturnResult("irsend -d /var/run/lirc/lircd-lirc0 SEND_ONCE Samsung channel+") </code></pre> <p>I have tried Google..but to no avail. Can anyone help me understanding the issue?</p> <p>//edit on request:</p> <p>my SysExec.h file looks like this:</p> <pre><code>#include "CompileFlags.h" /* * sysexec.h * * Created on: Dec 19, 2012 * Author: Shrouk H. Khan / Fingi / root */ #ifndef SYSEXEC_H_ #define SYSEXEC_H_ #include&lt;stdio.h&gt; #include &lt;string&gt; #include &lt;boost/algorithm/string.hpp&gt; namespace fingigcc { class SysExec { public: SysExec(); virtual ~SysExec(); static std::string executeAndReturnResult(char* cmd); static int executeAndForget(char* cmd); static int execute(char* cmd); }; } /* namespace fingigcc */ #endif /* SYSEXEC_H_ */ </code></pre> <p>And the corresponding cpp file looks like this:</p> <pre><code>/* * SysExec.cpp * * Created on: Dec 19, 2012 * Author: Shrouk H. Khan / Fingi / root */ #include "../../includes/SysExec.h" #include "../../includes/Logger.h" #include &lt;cstdlib&gt; #include &lt;iostream&gt; #include &lt;stdio.h&gt; #include &lt;unistd.h&gt; namespace fingigcc { SysExec::SysExec() { // TODO Auto-generated constructor stub } SysExec::~SysExec() { // TODO Auto-generated destructor stub } std::string SysExec::executeAndReturnResult(char* cmd) { //cout &lt;&lt; "[SHELL] : " &lt;&lt; cmd &lt;&lt; endl; FILE* pipe = popen(cmd, "r"); if (!pipe) return "ERROR"; char buffer[128]; std::string result = ""; while (!feof(pipe)) { if (fgets(buffer, 128, pipe) != NULL) result += buffer; } pclose(pipe); return result; } int SysExec::executeAndForget(char* cmd) { //cout &lt;&lt; "[SHELL] : " &lt;&lt; cmd &lt;&lt; endl; //do not try to log it..infinite loop.. // pid_t pid = fork(); // if (pid &lt; 0) { // return -1; // } else if (pid == 0) { // execl(cmd, (char *) 0); // } else { // // } system(cmd); return 0; } } /* namespace fingigcc */ </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload