Note that there are some explanatory texts on larger screens.

plurals
  1. PO$? inside bash trap
    primarykey
    data
    text
    <p>From a bash script, I'm trying to handle segmentation faults from a c++ program. I've read that using <code>trap</code> on <code>SIGCHLD</code> can be used for this purpose. Inside the trap I should be able to test for <code>$?</code> to get the return code from the program. See <a href="https://unix.stackexchange.com/questions/24307">https://unix.stackexchange.com/questions/24307</a> for example.</p> <p>It's not working for me, and I can't figure out why.</p> <p>Here is the script:</p> <pre><code>#! /bin/bash set -bm trap 'echo "Trap result code $?"' CHLD echo "Script: starting program" ./sigsegv echo "Script: result code from program was $?" </code></pre> <p>As you might guess, the <code>sigsegv</code> program just causes a segfault:</p> <pre><code>#include &lt;csignal&gt; #include &lt;iostream&gt; using namespace std; int main(int argc, char** argv) { cout &lt;&lt; "C++ will now cause a SIGSEGV" &lt;&lt; endl; raise(SIGSEGV); cout &lt;&lt; "After signal -- shouldn't get here" &lt;&lt; endl; return 0; } </code></pre> <p>When I run the wrapper script, I get this:</p> <pre><code>$./wrappersimple.sh Script: starting program C++ will now cause a SIGSEGV Trap result code 0 ./wrapper.sh: line 8: 26368 Segmentation fault (core dumped) ./sigsegv Script: result code from program was 139 </code></pre> <p>The key piece was <code>Trap result code 0</code>. That's where I expected it to say 139 to indicate a SIGSEGV (which is 128 base value + 11 for SIGSEGV).</p> <p>In case it matters, RHEL 6.2, bash 4.1.2(1)-release.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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