我一直在尝试编写一个bash脚本来运行我的代码,然后将最终输出与regression.dat下的先前运行(具有正确的结果)进行比较。它的目标是告诉我代码是否在与以前相同的迭代次数中收敛,以及结果是否在 .dat 文件中的基准结果的 0.1% 范围内。到目前为止,我已经收到很多语法错误,即使我摆脱了它们,变量 reg1、reg2、reg3 等也只会将 0 存储为值
#!/bin/bash
rm *.dat
rm *.vtu
FiniteVolume.serial export FORT_FMT_RECL=250
mv finaloutput.dat regression
cd regression
readarray filecontent < regression_test.dat
echo $filecontent
readarray filecontent2 < finaloutput.dat
echo $filecontent2
((reg1=${filecontent[1]}))
((reg2=${filecontent[2]}))
((reg3=${filecontent[3]}))
((reg4=${filecontent[4]}))
echo reg1
echo $reg1
((exm1=${filecontent2[1]}))
((exm2=${filecontent2[2]}))
((exm3=${filecontent2[3]}))
((exm4=${filecontent2[4]}))
if ((reg1 = exm1)); then
echo "iteration count is equal"
elif ((sqrt((reg2- exm2)^2)/reg2 < 1e-1)) ; then
echo "Lift coefficient error is less than .1 percent"
elif ((sqrt((reg2- exm2)^2)/reg2 > 1e-1)); then
echo "Lift Coefficient test failed"
elif ((sqrt((reg3- exm3)^2)/reg3 < 1e-1)); then
echo "Drag coefficient error is less then .1 percent"
elif ((sqrt((reg3- exm3)^2)/reg3 > 1e-1)); then
echo "Drag Coefficient test failed"
elif ((sqrt((reg4- exm4)^2)/reg4 < 1e-1)); then
echo "Residual error is less than .1 percent"
elif ((sqrt((reg2- exm2)^2)/reg2 > 1e-1)); then
echo "Residual test failed"
fi
cd ..
rm *.dat
rm *.vtu
结果如下:
14159 0.39700296920172 9.560746238889790E-002 9.992898637502570E-013
14159 0.39700296920172040 9.56074623888978209E-002 9.99311202935717304E-013
./regression_test.sh: line 11: reg1=: syntax error: operand expected (error token is "=")
./regression_test.sh: line 12: reg2=: syntax error: operand expected (error token is "=")
./regression_test.sh: line 13: reg3=: syntax error: operand expected (error token is "=")
./regression_test.sh: line 14: reg4=: syntax error: operand expected (error token is "=")
reg1
./regression_test.sh: line 25: ((: sqrt((reg2- exm2)^2)/reg2 < 1e-1: syntax error in expression (error token is "((reg2- exm2)^2)/reg2 < 1e-1")
./regression_test.sh: line 27: ((: sqrt((reg2- exm2)^2)/reg2 > 1e-1: syntax error in expression (error token is "((reg2- exm2)^2)/reg2 > 1e-1")
./regression_test.sh: line 29: ((: sqrt((reg3- exm3)^2)/reg3 < 1e-1: syntax error in expression (error token is "((reg3- exm3)^2)/reg3 < 1e-1")
./regression_test.sh: line 31: ((: sqrt((reg3- exm3)^2)/reg3 > 1e-1: syntax error in expression (error token is "((reg3- exm3)^2)/reg3 > 1e-1")
./regression_test.sh: line 33: ((: sqrt((reg4- exm4)^2)/reg4 < 1e-1: syntax error in expression (error token is "((reg4- exm4)^2)/reg4 < 1e-1")
./regression_test.sh: line 35: ((: sqrt((reg2- exm2)^2)/reg2 > 1e-1: syntax error in expression (error token is "((reg2- exm2)^2)/reg2 > 1e-1")
如何更改脚本以消除语法错误并允许变量采用我将读取数据存储到的数组的元素值?任何帮助将不胜感激,因为我在过去的一天里阅读了 bash 算术和脚本,但无法得到执行此操作的最佳方法的明确解释。
答案1
bash
没有内置的浮点数学,我也不相信它提供平方根。对于这些,您需要调用外部实用程序,例如bc
。
用不同的语言编写此代码可能会更好,例如 Perl 或 Python;这似乎已经接近 bash 处理不好的复杂性阈值了。