I am not much familiar with shell script. I want to write a shell script for the following pseudo-code:
min=some garbage value
for(i=1 to N){ // N and n will be taken as input for the shell script.
for(j=1 to n){
val= './a.out' // call the executable a.out and take the output in val
if(val<min) // a.out is a random number generator script
min=val;
}
arr[k++]=min;
}
// Then I want to calculate the sum of the elements of the array 'arr'.
How to do these by a shell script?
a.out
is such that it will produce a string of digits as output. I have to convert this string into the variable val
. How to do this? For example, a.out
is producing 12.34
as output but 12.34
is a character array and not a float. I want to convert this to float through shell script and I don't have permission to change the C code for which a.out
is the executable.
#!/bin/bash
# set min to some garbage value
min=1
N=$1
n=$2
for (( i=1; i<=$N; i++ )); do
for (( j=1; j<=$n; j++ )); do
val=$(/path/to/a.out)
val2=`echo $val | bc` // is this the correct syntax?
if (( $val2 < $min )); then
min=$val2;
fi
done
arr=("${arr[@]}" "$min")
done
# Then I want to calculate the sum of the elements of the array 'arr'.
sum=0
for (( l=0; l<${#arr[@]}; l++ )); do
sum=$( expr $sum + ${arr[$l]} )
done
echo "Sum of \$arr = ${sum}"
The above code is not working for me.