Checking C ++ executable return value through shell script

I run the shell script in windows with cygwin, in which I execute the program several times with different arguments each time. Sometimes a program generates a segmentation error for some input arguments. I want to generate a text file in which a shell script can write for which of the inputs the program failed. Basically, I want to check the return value of the program every time it starts. Here I assume that when a program fails, it returns a different value from when it succeeds. I am not sure about that. The executable file is a C ++ program.

Can this be done? Please guide. If possible, provide a code snippet for a shell script.

Also, please say that all values ​​are returned.

My script is a .sh file.

+4
source share
4 answers

You can check the return value with the shell if command:

 if program; then echo Success else echo Fail fi 

or using the "and" or "or" lists to execute additional commands only if your success or failure:

 program && echo Success program || echo Fail 

Note that the test succeeds if the program returns 0 for success, which is a little contrary to intuition if you are used to C / C ++ conditions for non-zero values.

0
source

The return value of the last completed program is available in the environment variable $? .

+3
source

if it is a bat file, you can use% ERRORLEVEL%

0
source

Assuming no significant spaces in command line arguments:

 cat <<'EOF' | -V -h -: -a whatnot peezat ! while read args do if program $args then : OK else echo "!! FAIL !! ($?) $args" >> logfile fi done 

It takes more effort (to be polite about this) if you must keep spaces. Well, a little more effort; you are probably using eval before the "program".

0
source

Source: https://habr.com/ru/post/1303003/


All Articles