0

I've been trying to write a bash script to run my code, then compare the final output to a previous run (with the correct result) under regression.dat. The goal is for it to tell me if the code converges in the same number of iterations as before and if the results are within .1 percent of the benchmark results in the .dat file. So far I've been receiving lots of syntax errrors, and even when I got rid of them, the variables reg1, reg2, reg3, etc. would only store 0 as a value

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

The results are as follows:

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")

How do I change the script to get rid of the syntax errors and to allow the variables to take on the values of elements the arrays that I'm storing the read data to? any help would be appreciated as I've spent the past day reading over bash arithmetic and scripting and haven't been able to get a clear explanation of the best way to do this.

1

bash does not have built-in floating-point math, nor I believe does it provide square roots. For these, you need to call an external utility, e.g. bc.

It may be better to write this code in a different language, e.g. Perl or Python; this is nearing the threshold of complexity which bash doesn't handle that well, it seems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.