title: How can I get the return value of `wget` from a sh script into an `int` variable
body: <p>OS: Linux raspberrypi 4.19.58-v7l+ #1245 SMP Fri Jul 12 17:31:45 BST 2019 armv7l GNU/Linux
  Board: Raspberry Pi 4</p>
  
  <p>I have a script:</p>
  
  <pre><code>#!/bin/bash
  
  line=$(head -n 1 /var/www/html/configuration.txt)
  file=/var/www/html/4panel/url_response.txt
  if [ -f "$file" ]; then
      wget_output=$(wget -q -i "$line" -O $file --timeout=2)
      echo "$?"
  else
      echo &gt; $file
      chown pi:pi $file 
  fi
  </code></pre>
  
  <p>which I call from a C++ program using:</p>
  
  <pre><code>int val_system = 0;
  val_system = system("/var/www/html/4panel/get_page.sh");
  std::cout&lt;&lt;"System return value: "&lt;&lt;val_system&lt;&lt;std::endl;
  </code></pre>
  
  <p>If there is something wrong with the script, <code>echo "$?"</code> will output the return value of <code>wget</code>, but <code>val_system</code> will always be 0.</p>
  
  <p>Does <code>system()</code> returns the value of <code>echo "$?"</code> ? In which case 0 is correct. And if that is the case how can I put the return value of <code>wget</code> in <code>val_system</code> ?</p>
  
  <p>I have taken a situation in which <code>echo "$?"</code> always returns 8, basically I've entered an incorrect URL and:</p>
  
  <ul>
  <li>I have tried deleting <code>echo "$?"</code> but <code>val_system</code> still returned 0;</li>
  <li>With <code>echo "$?"</code> deleted I have changed the <code>wget</code> line to <code>wget -q -i "$line" -O $file --timeout=2</code> and <code>val_system</code> now returns 2048.</li>
  </ul>
  
  <p>None of my attempts bared any fruit and I have come here to seek guidance. How can I make <code>val_system</code> / <code>system()</code> return what <code>echo "$?"</code> returns ?</p>
  
  <p>How can I get the return value of <code>wget</code> from the script into an <code>int</code> variable that's inside the C++ program that calls the script ?</p>