How to be noticed if calling a program caused segmentation fault in a linux bash script, probably to stop the script then?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If the program exits with a segmentation fault, or any other error, it will exit with a non-zero exit code. You can test this exit code, and exit your script when it does so. If you want to stop on any error (not just segmentation fault), you can use:
If you want to exit your script if any program that you call returns an error (except for as part of an
iforwhilestatement), you can just callset -eat the beginning of your script, to cause the script to exit immediately if any command fails. This usage is somewhat discouraged in larger scripts that need to be maintained over time, since it can lead to your script exiting at an unexpected time if something likegrepreturns a non-zero exit code, but it can be useful for quick one-off scripts if you know that you always want to stop on error.If you only want to exit if the program crashed with a segfault, not any other error, you can check the specific exit code. On most systems,
SEGVhas value 11, but you can check with:Then add 128 to that, and that will be the exit code your program exits with. Test the exit code against that to find out if your program crashed with SIGSEGV: