4

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

Is it possible to do this? Please guide. If possible, please provide a code snippet for shell script.

Also, please tell what all values are returned.

My script is .sh file.

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

Comments

0

You can test the return value using shell's if command:

if program; then
    echo Success
else
    echo Fail
fi

or by using "and" or "or" lists to do extra commands only if yours succeeds or failed:

program && echo Success
program || echo Fail

Note that the test succeeds if the program returns 0 for success, which is slightly counterintuitive if you're used to C/C++ conditions succeeding for non-zero values.

Comments

0

if it is bat file you can use %ERRORLEVEL%

Comments

0

Assuming no significant spaces in your 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

This takes a but more effort (to be polite about it) if you must retain spaces. Well, a bit more effort; you probably use an eval in front of the 'program'.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.