I have a shell script to run several times a .c program ('switch') which admit 3 input paramenters.
I want to run the program 4 times passing values {4,16,32,64}
, and for parameters
{0.1,0.2,.3,.4,.5,.55,.575,.6,.625,.65,.7,.75,.8,.9}
, building a table of average and confidence interval.
The script is the following
#!/bin/bash
if [ -e salida.txt ]; then
# echo "File exists"
rm salida.txt
fi
touch salida.txt
touch sal1.txt
touch sal2.txt
touch sal3.txt
touch sal4.txt
touch salida.txt
num=1000000
stud=3.182
for j in {0.1,0.2,.3,.4,.5,.55,.575,.6,.625,.65,.7,.75,.8,.9}
do
rm sal1.txt sal2.txt sal3.txt sal4.txt
for i in {4,16,32,64}
do
margen=0
sum=0
avg=0
for k in {1..4}
do
a$k=$((`./switch -N$i -r$j -n$num`))
sum=$((sum + a$k))
done
avg=$((sum/4))
dvt=0
for k in {1..4}
do
dvt=$((dvt + (a$k - $avg)*(a$k - $avg)))
done
dvt=$((dvt /3))
dvt=$((echo `sqrt($dvt) | bc -l`))
margen=$((dvt*$stud/2))
echo $avg $margen >> sal$i.txt
done
join sal1.txt sal2.txt sal3.txt sal4.txt >> salida.txt
done
Nevertheless is giving me error in line 27
a$k=$((`./switch -N$i -r$j -n$num`))
and lines
dvt=$((echo `sqrt($dvt) | bc -l`))
margen=$((dvt*$stud/2))
What am I doing wrong?
a$k=$((
./switch -N$i -r$j -n$num))
is not a good var name - the parser won't handle it correctly in that context without aneval
. Better, though, would be to assign it like:: $((a$k=$(./switch "-N$i" "-r$j" "-n$num")))
.a$k
only lasts as long as you're in the for loop where you're setting them. You need to set them outside the for loop prior if you want to use them later on.a$k
. It looks like the errors you mention for the latter two lines will likely be division by zero errors. The shell doesn't expand variables in assignment contexts - left or right side of the equal sign they remain unexpanded. Soa$k=?
doesn't work, and your references to it in$dvt
leave it with a zero-value, I bet.chknm() { return "$(($1$$=0))"; }; chknm "$var" && eval "_$var=\$val"
.