Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

System does not print stderr

I have the following Ruby code:

system "clang test.c -o test"
system "./test"

When I execute the above Ruby code, the stdout is printed, but the stderr is not.

When I run clang test.c -o test && ./test from the terminal, I get a segmentation fault, but the Ruby script does not print this.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can I get the Ruby script to print all output from the system command?

>Solution :

The output "segmentation fault" is not printed by your test program itself. If you see this in your shell, it is in fact printed by the shell itself as a result of your program segfaulting.

Ruby does not print this. You can check the return code of your program after the system cal returned however using the $? variable which contains a Process::Status object. Here, you can e.g. check for signaled? to check if the process aborted due to a signal and then get this signal with the stopsig method. The signal number for a segmentation fault is 11.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading