Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash git pre-commit hook running MatLab function does not wait for a result

I have a MatLab project on Win-7.

Its version controled with Git-Extension.

I have a matlab function that runs a kind of self-test.

It's named pre_push_test_suit, and it exits with either code 0 (OK) or 1 (there is a problem).

I want to implement the pre-commit git hook, that will prevent pushing to the central repo if the self-test function fails.

my script starts like this:

#!/bin/sh
res_file=pre_push_test_log.txt
resultcode=$(matlab -automation -minimize -r pre_push_test_suit -logfile $res_file)
if [ "$resultcode" -eq "0" ]
...

Expected result: I wanted the script to lunch MatLab and wait for exit value.

Actual result: the script lunches MatLab process, and continues with empty value in $resultcode.

If I got it correctly, the script is run on a bash-like shell that is installed with git on windows, but I'm not sure it is a real bash.

Typing ps did not show the matlab process.

Also tried, but with no change of result:

  • to replace the $(...) with ...
  • to add "wait" after the third line
  • google it.

I did not try scripts in any other language but bash (I do not know many scripting languages).

I thought about the ugly solution of infinite loop with "wait", waiting for a file to contain some output, but i prefer something more decent.

Any better solution to wait for the result, in any language, is welcome.

like image 948
Oded Avatar asked Apr 25 '26 08:04

Oded


1 Answers

It seems the problem occurs because matlab automatically detaches itself from the shell in windows (this behaviour doesn't happen on Linux with the -nodesktop option).

Option 1:

Use the -wait option:

From Docs

Wait for MATLAB to Terminate

By default, when you call the matlab command from a script, the command starts MATLAB and then immediately executes the next statements in the script. The -wait option pauses the script until MATLAB terminates. Option Result

-wait

Use in a startup script to process the results from MATLAB. Calling MATLAB with this option blocks the script from continuing until the results are generated.

#!/bin/sh

res_file=pre_push_test_log.txt

resultcode=$(matlab -wait -nodesktop -minimize -r pre_push_test_suit -logfile $res_file) 


if [ "$resultcode" -eq "0" ]

Option 2:

use octave instead of matlab

Option 3:

use wait to wait for the process to be finished: How to wait in bash for several subprocesses to finish and return exit code !=0 when any subprocess ends with code !=0?

the -nodesktop option should keep the matlab session inside terminal's control.

#!/bin/sh

res_file=pre_push_test_log.txt

resultcode=$(matlab -nodesktop  -minimize -r pre_push_test_suit -logfile $res_file) 
wait $!

if [ "$resultcode" -eq "0" ]

[there are problems with option 3, see comments]

like image 95
patapouf_ai Avatar answered Apr 28 '26 04:04

patapouf_ai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!