<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How to verify Linux &quot; system &quot; call  results ?]]></title><description><![CDATA[<p dir="auto">I am embarrassed to post this, but...</p>
<ol>
<li>how can I verify - using "system" call  that file exist ?</li>
<li>how can I redirect "system" call actual text / error returns to QT widget ?</li>
</ol>
<p dir="auto">I  know how to read the pass / fail returns , no problem .<br />
I need the actual valid text or error to show in widget.</p>
<p dir="auto">I know it can be done because of "stdio"  redirecting , but totally  forgot how.</p>
<pre><code>    text = " mkdir /tmp";
    array = text.toLocal8Bit();
    buffer = array.data();
    result = system(buffer);
</code></pre>
<p dir="auto">and it trows this error to console, I want to redirect it to widget.</p>
<p dir="auto">mkdir: cannot create directory ‘/tmp’: File exists</p>
<p dir="auto">Thanks</p>
]]></description><link>https://forum.qt.io/topic/144905/how-to-verify-linux-system-call-results</link><generator>RSS for Node</generator><lastBuildDate>Tue, 23 Jun 2026 16:50:52 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/144905.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 05 May 2023 15:56:56 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to verify Linux &quot; system &quot; call  results ? on Sun, 07 May 2023 04:52:37 GMT]]></title><description><![CDATA[<p dir="auto">Here is an update.<br />
Went back to project backup copy and got  rid of the gibberish.</p>
<p dir="auto">The result of the "ln" (  '/dev/rfcomm0' -&gt; '/dev/ttyUSB0' ) is now in the temporary file<br />
which resides in my main .pro folder .<br />
Do not like it there , but...</p>
<p dir="auto">I like to redirect good "ln" result  into  QString using stdio - if possible-<br />
,and error too.<br />
I am working on that but could still use some assistance  to add that to the current command.</p>
<p dir="auto">"TASK  unlink "<br />
" echo q | sudo -S -k  unlink  /dev/rfcomm0  "<br />
[sudo] password for nov25-1: " echo q | sudo -S -k  unlink  /dev/rfcomm0   SUCCESS   un linked "<br />
" Result system 0"<br />
"TASK  link - add tee "<br />
" echo q | sudo -S -k  ln -s -v   /dev/ttyUSB0  /dev/rfcomm0   | tee temporary_link_file.txt"<br />
[sudo] password for nov25-1: '/dev/rfcomm0' -&gt; '/dev/ttyUSB0'<br />
" echo q | sudo -S -k  ln -s -v   /dev/ttyUSB0  /dev/rfcomm0   | tee temporary_link_file.txt SUCCESS   linked "<br />
" Result system 0"</p>
]]></description><link>https://forum.qt.io/post/757339</link><guid isPermaLink="true">https://forum.qt.io/post/757339</guid><dc:creator><![CDATA[Anonymous_Banned275]]></dc:creator><pubDate>Sun, 07 May 2023 04:52:37 GMT</pubDate></item><item><title><![CDATA[Reply to How to verify Linux &quot; system &quot; call  results ? on Sun, 07 May 2023 00:39:19 GMT]]></title><description><![CDATA[<p dir="auto">@AnneRanch said in <a href="/post/757310">How to verify Linux " system " call results ?</a>:</p>
<blockquote>
<p dir="auto">PS<br />
I do not get why everybody is always concern with "stalling the main process" -<br />
it does not matter if the main process is blocked -  it cannot<br />
function until the "child process" is finished anyway.</p>
</blockquote>
<p dir="auto">because the essense of event driven programming is an event loop that needs to be continually running.  You're still thinking in procedural progamming terms, not event driven terms.  It's doing a lot of "stuff" behind the scenes that you don't see...that's one of the reasons we use a framework instead of doing everything brute force.  it does a lot of the housekeeping for you behind the scenes.</p>
]]></description><link>https://forum.qt.io/post/757334</link><guid isPermaLink="true">https://forum.qt.io/post/757334</guid><dc:creator><![CDATA[Kent-Dorfman]]></dc:creator><pubDate>Sun, 07 May 2023 00:39:19 GMT</pubDate></item><item><title><![CDATA[Reply to How to verify Linux &quot; system &quot; call  results ? on Sat, 06 May 2023 18:41:45 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a><br />
Thanks everybody for all the comments. Appreciate that very much.</p>
<p dir="auto">As of right now I actually have basic issue with just the "command".</p>
<p dir="auto">I am getting confusing and conflicting results.</p>
<p dir="auto">"echo q | sudo -S -k unlink  /dev/rfcomm0 "<br />
sh: 1: ��x3��t: not found<br />
sh: 1: ��tn�S���v~��ty��tL��tG��tB��t=�PŃ�D�H��[]A�: not found<br />
"echo q | sudo -S -k unlink  /dev/rfcomm0  FAILURE   not un linked "</p>
<p dir="auto">Since  /dev/rfcomm0 does not yet exists the above is OK, I have no idea where the gobberish comes from.</p>
<p dir="auto">"echo q | sudo -S -k ln -s -v  /dev/ttyUSB  /dev/rfcomm0  | tee temp_link.txt | tee temp_link_copy.txt"<br />
[sudo] password for nov25-1: ln: failed to create symbolic link '/dev/rfcomm0': File exists</p>
<p dir="auto">Now this is wrong - contradicts "unlink" command.<br />
I need to find the reason why.</p>
<p dir="auto">"echo q | sudo -S -k ln -s -v  /dev/ttyUSB  /dev/rfcomm0  | tee temp_link.txt | tee temp_link_copy.txt SUCCESS   linked "</p>
<p dir="auto">I am actually unable to determine , from the command itself , where the "tee" creates the file(s).</p>
<p dir="auto">I did try</p>
<p dir="auto">text += " | tee  /home/temp_link.txt ;</p>
<p dir="auto">but it requires another authorization and I just do not know how<br />
to add another "sudo" to the already convoluted<br />
command.</p>
<p dir="auto">I would actually like to have the temp file in main .pro folder.<br />
That is my <strong>main task</strong> right now,<br />
I will  decide between QProcess or system after I get<br />
the commands to behave.</p>
<p dir="auto">PS<br />
I do not get why everybody is always concern with "stalling the main process" -<br />
it does not matter if the main process is blocked -  it cannot<br />
function until the "child process" is finished anyway.<br />
It would work just fine, for my application, if I process the "command"<br />
in parent and just skip creating the "child".</p>
]]></description><link>https://forum.qt.io/post/757310</link><guid isPermaLink="true">https://forum.qt.io/post/757310</guid><dc:creator><![CDATA[Anonymous_Banned275]]></dc:creator><pubDate>Sat, 06 May 2023 18:41:45 GMT</pubDate></item><item><title><![CDATA[Reply to How to verify Linux &quot; system &quot; call  results ? on Sat, 06 May 2023 09:00:55 GMT]]></title><description><![CDATA[<p dir="auto">As said before.  You can do <em>much</em> more interacting with <code>QProcess</code> than with <code>system()</code>.  The latter simply runs <code>/bin/sh -c "your command with any arguments"</code> and waits for it to finish.  There is nothing it does which cannot be done via <code>QProcess</code>.  Effectively the <code>static</code> function <code>QProcess::execute("/bin/sh", { "-c",  "your command with any arguments"</code> })' does just the same thing.</p>
<p dir="auto">Both of these suffer because any output from the command is written to stdout/stderr, and you cannot access those without doing redirection from the calling program (which we could do from the Qt application, but it's an effort we don't need).  So for example you will not see any error output which is what you are asking for.</p>
<p dir="auto">However, with <code>QProcess</code> the following <em>does</em> allow to access any output:</p>
<pre><code>// next 3 lines do the same as `system("mkdir /tmp")`
QProcess p;
p.start("/bin/sh", { "-c", "mkdir /tmp" });
qDebug() &lt;&lt; p.waitForFinished();

// next 2 lines read any output, which cannot be done so easily from `system()`
qDebug() &lt;&lt; p.readAllStandardOutput();
qDebug() &lt;&lt; p.readAllStandardError();
// or above two lines can be replaced by `qDebug() &lt;&lt; p.readAll()` if you don't want to see stdout/stderr separately
</code></pre>
<p dir="auto">I imagine <code> p.readAllStandardError();</code> will report something like <code>/tmp: Directory already exists</code> here.</p>
<p dir="auto">If you always use this <code>QProcess</code> pattern and <em>never</em> use <code>system()</code> in a Qt application you will always be able to access a command's output, plus write to its input if wanted, with no disadvantages.</p>
]]></description><link>https://forum.qt.io/post/757268</link><guid isPermaLink="true">https://forum.qt.io/post/757268</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sat, 06 May 2023 09:00:55 GMT</pubDate></item><item><title><![CDATA[Reply to How to verify Linux &quot; system &quot; call  results ? on Sat, 06 May 2023 03:16:32 GMT]]></title><description><![CDATA[<p dir="auto">@AnneRanch</p>
<p dir="auto">With respect, QProcess is the better approach to access system shell commands, albeit more complicated.</p>
<p dir="auto">system() (POSIX call) stalls the running program until the child terminates.</p>
<p dir="auto">As long as you are aware of that, and can accept the consequences.</p>
<p dir="auto"><strong>man 3 system</strong> explains it...It forks, does an execl(), and then waits for the pid to terminate before the parent continues.</p>
<p dir="auto">As <a class="plugin-mentions-user plugin-mentions-a" href="/user/paul-colby">@<bdi>Paul-Colby</bdi></a> mentions, there are calls that check for file exists, so the above comments may not be relevant, but it is important to understand that system() stalls the parent.</p>
]]></description><link>https://forum.qt.io/post/757262</link><guid isPermaLink="true">https://forum.qt.io/post/757262</guid><dc:creator><![CDATA[Kent-Dorfman]]></dc:creator><pubDate>Sat, 06 May 2023 03:16:32 GMT</pubDate></item><item><title><![CDATA[Reply to How to verify Linux &quot; system &quot; call  results ? on Sat, 06 May 2023 01:44:40 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/paul-colby">@<bdi>Paul-Colby</bdi></a> You are "the man " Nice , very much needed to continue my project.<br />
BTW<br />
I have been using QProcess and decided to switch / try  "system"  - since  they are both based on same.<br />
My concerns about QProcess - when I started I have been told that I really cannot interact with QProcess.<br />
I have been experimenting with "ready to read"  time and "timeout" and found  that when my command actually fails to execute my code  fails "ready to read" and goes into "timeout" immediately.<br />
So my next step was  to identify  such false timeout  failure as the  command failure...</p>
<p dir="auto">It seems that "system" may do that better, after I digest you last post.</p>
]]></description><link>https://forum.qt.io/post/757261</link><guid isPermaLink="true">https://forum.qt.io/post/757261</guid><dc:creator><![CDATA[Anonymous_Banned275]]></dc:creator><pubDate>Sat, 06 May 2023 01:44:40 GMT</pubDate></item><item><title><![CDATA[Reply to How to verify Linux &quot; system &quot; call  results ? on Sat, 06 May 2023 00:24:07 GMT]]></title><description><![CDATA[<p dir="auto">Hi @AnneRanch,</p>
<blockquote>
<p dir="auto">I am embarrassed to post this, but...</p>
</blockquote>
<p dir="auto">Nothing to be embarrassed about.</p>
<p dir="auto">Before going any further, I must say: You really, really, should consider using something like <a href="https://doc.qt.io/qt-6/qprocess.html" target="_blank" rel="noopener noreferrer nofollow ugc">QProcess</a> for this - it will save you a huge amount of re-invention effort! :D</p>
<p dir="auto">That said, if you must use old Standard C library functions...</p>
<blockquote>
<ol>
<li>how can I verify - using "system" call  that file exist ?</li>
</ol>
</blockquote>
<p dir="auto">There are so many ways. If you <em>must</em> use <code>system()</code>, then maybe use <code>stat</code> like:</p>
<pre><code>auto result = system("stat /tmp &gt; /dev/null 2&gt;&amp;1");
</code></pre>
<p dir="auto">But it would be better to use the (Standard C) <code>fstat()</code> function directly, rather than going through <code>system()</code>, and much, much, much better to use <a href="https://doc.qt.io/qt-6/qfile.html#exists" target="_blank" rel="noopener noreferrer nofollow ugc">QFile::exits()</a> instead.</p>
<blockquote>
<ol start="2">
<li>how can I redirect "system" call actual text / error returns to QT widget ?</li>
</ol>
</blockquote>
<p dir="auto">You can't from outside the <code>system()</code> call - you need to do the redirection inside the executed command itself, eg:</p>
<pre><code>system("mkdir /tmp &gt; my-comands-stdout.txt 2&gt; my-commands-stderr.txt");
</code></pre>
<p dir="auto">Of course then you need to clean up the temporary files afterwards. And if performance matters (unlikely with <code>mkdir</code>) then you'd want to <code>mkfifo()</code> for the output files.  <code>system()</code> is essentially a convenience wrapper around <code>popen()</code> or <code>execl()</code>, and the above is effectively working around the shortcuts <code>system()</code> provides, so, you would be much better off switching to using <code>popen()</code> instead of <code>system()</code>.</p>
<p dir="auto">And again, you would be much, much, <strong>much</strong> better of just using <a href="https://doc.qt.io/qt-6/qprocess.html" target="_blank" rel="noopener noreferrer nofollow ugc">QProcess</a> instead!</p>
<blockquote>
<p dir="auto">mkdir: cannot create directory ‘/tmp’: File exists</p>
</blockquote>
<p dir="auto">Just a side note, you might want to use <code>mkdir</code>'s <code>-p</code> option.  But in this case, I would super-strongly recommend <a href="https://doc.qt.io/qt-6/qdir.html#mkpath" target="_blank" rel="noopener noreferrer nofollow ugc">QDir::mkpath()</a> instead.</p>
<p dir="auto">Cheers.</p>
]]></description><link>https://forum.qt.io/post/757256</link><guid isPermaLink="true">https://forum.qt.io/post/757256</guid><dc:creator><![CDATA[Paul Colby]]></dc:creator><pubDate>Sat, 06 May 2023 00:24:07 GMT</pubDate></item></channel></rss>