1

OS: Linux raspberrypi 4.19.58-v7l+ #1245 SMP Fri Jul 12 17:31:45 BST 2019 armv7l GNU/Linux Board: Raspberry Pi 4

I have a script:

#!/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 > $file
    chown pi:pi $file 
fi

which I call from a C++ program using:

int val_system = 0;
val_system = system("/var/www/html/4panel/get_page.sh");
std::cout<<"System return value: "<<val_system<<std::endl;

If there is something wrong with the script, echo "$?" will output the return value of wget, but val_system will always be 0.

Does system() returns the value of echo "$?" ? In which case 0 is correct. And if that is the case how can I put the return value of wget in val_system ?

I have taken a situation in which echo "$?" always returns 8, basically I've entered an incorrect URL and:

  • I have tried deleting echo "$?" but val_system still returned 0;
  • With echo "$?" deleted I have changed the wget line to wget -q -i "$line" -O $file --timeout=2 and val_system now returns 2048.

None of my attempts bared any fruit and I have come here to seek guidance. How can I make val_system / system() return what echo "$?" returns ?

How can I get the return value of wget from the script into an int variable that's inside the C++ program that calls the script ?

bleah1
  • 471
  • 3
  • 18
  • do you use macos? – Mihir Luthra Sep 17 '19 at 09:39
  • No. It's `Linux raspberrypi 4.19.58-v7l+ #1245 SMP Fri Jul 12 17:31:45 BST 2019 armv7l GNU/Linux` – bleah1 Sep 17 '19 at 09:40
  • 2
    See [returning multiples of 256](https://stackoverflow.com/questions/26375514/call-system-return-256) – Mihir Luthra Sep 17 '19 at 09:46
  • 2
    I tested the same on `macos` and `freebsd`, and yea this was new to see return values in multiples of 256. So what I see is when I do `exit 1`, it returns 256, when `exit 2`, it returns 512 and so on. Check one of those answers in the link above, it explains the reason. – Mihir Luthra Sep 17 '19 at 09:47
  • 1
    Great catch ! You may be right. I get 2048 as exit status, which means 256*8. And 8 is the "real" exit status of `wget`. I will test it more. I think you can formulate an answer based on this. – bleah1 Sep 17 '19 at 09:51
  • 1
    Yes. This is correct ! I have tried different exit status and they are all multiples of 256. – bleah1 Sep 17 '19 at 09:54

2 Answers2

4

The integer value system() returned contains extra information about executed command's status along with its exit code, see system() and Status Information. You need to extract exit code using WEXITSTATUS macro, like:

std::cout << "System return value: " << WEXITSTATUS(val_system) << std::endl;
oguz ismail
  • 1
  • 16
  • 47
  • 69
  • Uhm... I have written in the question that I have already tried that and it did not work. – bleah1 Sep 17 '19 at 09:30
  • That is the whole script and it does not return what it should. That's what I am saying. That's why I have posted here. – bleah1 Sep 17 '19 at 09:33
  • As I wrote in the question: if I enter an incorrect URL, `echo "$?"` returns 8 and if I delete `echo "$?"` then `system()` will return 2048 – bleah1 Sep 17 '19 at 09:37
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/199561/discussion-between-bleah1-and-oguz-ismail). – bleah1 Sep 17 '19 at 09:38
  • You should've kept the other answer as well, because it is correct – bleah1 Sep 17 '19 at 10:31
  • @bleah1 yeah, but it wasn't addressing the core issue – oguz ismail Sep 17 '19 at 10:45
3

If you want to echo the status and return it, you will need to save the value of $? to a variable, and exit with it explicitly.

wget_output=$(wget -q -i "$line" -O $file --timeout=2)
status=$?
...
echo $status
...
exit $status

If you don't need to execute echo or any other command between the call to wget and the end of the script, you can rely on the script exiting with the last status (i.e the one corresponding to the call to `wget) implicitly.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • Will try that `exit` stuff, but in the mean time, I still don't understand why `system()` doesn't return the proper value even with `echo "$"` deleted. – bleah1 Sep 17 '19 at 09:32