bash git预提交钩子运行MatLab函数不会等待结果
我有一个关于Win-7的MatLab项目 其版本由Git扩展控制 我有一个matlab函数,可以运行一种自检 它名为bash git预提交钩子运行MatLab函数不会等待结果,git,bash,matlab,shell,githooks,Git,Bash,Matlab,Shell,Githooks,我有一个关于Win-7的MatLab项目 其版本由Git扩展控制 我有一个matlab函数,可以运行一种自检 它名为pre_push_test_suit,退出时代码为0(确定)或1(出现问题) 我想实现预提交git钩子,如果自检功能失败,它将阻止推送到中央回购 我的脚本是这样开始的: #!/bin/sh res_file=pre_push_test_log.txt resultcode=$(matlab -automation -minimize -r pre_push_test_suit -l
pre_push_test_suit
,退出时代码为0(确定)或1(出现问题)
我想实现预提交git钩子,如果自检功能失败,它将阻止推送到中央回购
我的脚本是这样开始的:
#!/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" ]
...
预期结果:我希望脚本运行并等待退出值
实际结果:脚本暂停MatLab进程,并在$resultcode
中以空值继续
如果我没有弄错的话,这个脚本是在一个类似bash的shell上运行的,这个shell在windows上安装了git,但我不确定它是否是一个真正的bash
键入ps没有显示matlab过程
也尝试过,但结果没有改变:
- 将$(…)替换为
…
- 在第三行之后添加“wait”
- 谷歌
欢迎使用任何语言来等待结果的任何更好的解决方案。出现问题的原因似乎是matlab在windows中自动将自己从外壳中分离出来(这种行为在Linux上没有使用
-nodesktop
选项)
选项1:
使用-wait
选项:
从
等待MATLAB终止
默认情况下,从脚本调用matlab命令时
命令启动MATLAB,然后立即执行下一个命令
脚本中的语句。-wait选项将暂停脚本,直到
MATLAB终止。期权结果
-等等
在启动脚本中使用,以处理来自MATLAB的结果。使命感
使用此选项的MATLAB将阻止脚本继续,直到
生成结果
选项2:
用倍频程代替matlab
选项3:
使用“等待”等待流程完成:
-nodesktop
选项应将matlab会话保持在终端的控制范围内
#!/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" ]
[选项3存在问题,请参见注释]is没有尝试Octave,因为它需要在团队的每台机器上安装Octave,并且对MatLab和Octave之间的微小差异非常敏感(我在推前测试中运行了大量代码) 最后,我将MatLab函数write result(0表示成功)写入一个名为$(MatLab_file).txt的文件,实现了我前面提到的ugli循环 下面是我现在使用的代码:
#!/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
使用wait等待过程完成:
matlab
可能正在强制解除自身与控制终端的关联(作为一个窗口化的/etc.应用程序运行。你需要看看它是否有一个no daemon、no background或foreground命令行参数,你可以给它使它不这样做。很多做这类事情的程序都有这样一个选项。据我所知,OP等待文件包含信息,而不是等待进程完成OP做了一些丑陋的循环,我不在这里做。我不知道OP是如何在第三行之后添加等待的,但它一定是错误的,因为等待过程是有效的。没有“丑陋的循环”在OPs代码中。这是他们拒绝的潜在解决方案。不,wait
在强制分离自身的进程上不起作用。请尝试运行{sleep 5s&}&;wait
并告诉我您的wait
是否在等待任何东西。这就是为什么我给pid设置等待。由于SO是新的,我可能无法正确地形成它。但是:至于等待–我添加了等待行,就像上面的建议一样。它没有解决问题。至于-nodesktop选项,它包含在-automation选项中(请参阅)然而,我按照行中的建议尝试了这两种方法,然后编写了ps,但是没有新的进程ID(ps本身除外),所以现在有一个被shell识别的进程需要等待。为了确保我的操作正确,我对sleep做了同样的操作,并且在ps输出中有了一个新的条目。
#!/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