chutsu
chutsu

Reputation: 14103

How do I get non-zero return from a syntax error while running an octave script?

lets say I have a test.m octave script

test.m

x = 1;
A  % Syntax error, A isn't defined

so I run the script with

octave test.m

What I'm interested in is how do I get octave to return non-zero exit status if the script contains a syntax error? Note, I am not interested in encapulating the code with if-else statements and do exit(-1). I am interested in a solution that tells octave to return a non-zero value if the script contains syntax errors.

Edit: I was using Octave 4.2 that comes default with Ubuntu 18.04. Commenters have suggested Octave 5+ does not exhibit this behaviour.

enter image description here

Upvotes: 0

Views: 255

Answers (2)

Tasos Papastylianou
Tasos Papastylianou

Reputation: 22225

I cannot reproduce the behaviour in your question. In octave 5 at least, doing

octave myscript.m

results in a return value of 1 in case of error.

Same as doing

octave --eval "source('myscript.m')"

Upvotes: 0

chutsu
chutsu

Reputation: 14103

Never mind I solved it. To give some context why I needed this behaviour, its because I'm trying to write the octave scripts that basically test octave functions I write.

Now more often than not I will have written scripts that contain syntax errors, and octave will raise errors while parsing the scripts. However, the return status is always 0 if you run your script in a straight forward manner like:

octave script.m

regardless if your octave script contains syntax errors or not. I however did not want to surround every script I write with if-else or try-catches, that is why I opened this question on SO.

A workaround I have found is just to source the script by using the --eval flag. i.e.

octave --eval "source('script.m');"

This way if your script does contain syntax errors the return value / status is a non-zero exit value. In this case I believe octave defaults to 1.

Upvotes: 1

Related Questions