Bash git Executing binding using pre-commit MatLab function does not wait for result

I have a MatLab project on Win-7.

Its version is controlled by the Git-Extension.

I have a matlab function that does some sort of self-test.

It's named pre_push_test_suit

and it exits with code 0 (OK) or 1 (there is a problem).

I want to implement a pre-commit git binding that will prevent a push 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 go to MatLab lunch and wait for the exit value.

Actual result : the script dines on the MatLab process and continues with an empty value in $resultcode

.

If I understand correctly, the script runs on a bash-like shell installed with git on windows, but I'm not sure if this is real bash.

Typing ps did not show the matlab process.

Also tried, but without changing the result:

  • to replace $ (...) with ...

  • add "wait" after the third line
  • google it.

I haven't tried scripting in any other language but bash (I don't know many scripting languages).

I thought of an ugly infinite loop solution with "wait" expecting the file to contain some kind of output, but I prefer something more decent.

Any best solution to expect result in any language is appreciated.

+3


source to share


2 answers


The problem seems to be due to the fact that Matlab is automatically detached from the shell in windows (this behavior does not work on Linux with the option -nodesktop

).

Option 1:

Use the parameter -wait

:

From Documents

Wait for MATLAB to complete

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

-wait

Use a startup script to process results from MATLAB. invoking MATLAB with this option blocks the script from continuing until 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 a process to finish: How to wait in bash for multiple subprocesses to finish and return an exit code! = 0 when any subprocess ends up with a! = 0 code?

the parameter -nodesktop

must contain the matlab session inside the terminal.

#!/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]

+1


source


haven't tried Octave as it will take the Octave installation on every command machine and is sensitive to small differences between MatLab and Octave (I'm running a lot of code in test mode).

in the bottom line I made the result of writing the MatLab function (0 for success) to a file named $ (matlab_file) .txt, implemented the ugli loop mentioned earlier.



here is the code i am using now:

#!/bin/sh
matlab_file="pre_push_test_suit"
log_file="pre_push_test_log.txt"
res_file="${matlab_file}.txt"
rm -f $log_file
rm -f $res_file
sleep 1
matlab -automation -minimize -r $matlab_file -logfile $log_file
# wait for the result file to exist
until [ -f $res_file ]
do
     sleep 1
done
sleep 1
res_zero="$(grep 0 $res_file | wc -l)"
if [ $res_zero -eq "1" ]
    then
        echo "matlab pre_push_test_suit OK"
        exit 0
fi
echo "matlab pre_push_test_suit exited with an error"
echo "push is prevented"
exit 1

      

0


source







All Articles