Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
1  
Is this homework? –  user unknown Nov 7 '11 at 4:42
add comment

3 Answers 3

Val=$(./a.out)
Val=`a.out`

Both of these will execute a.out and store the output into Val.

Assuming bash or compatible shell.

share|improve this answer
    
Thanks for your reply. Actually, I want to know the exact "shell script" for my pseudo-code as totally unfamiliar with shell script. –  prasenjit Nov 7 '11 at 4:08
add comment

Here's your code translated to a script that should work with bash, or ksh:

#!/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)
       if (( $val < $min )); then
           min=$val; 
       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}"

Now, this is an actual translation of your code. You may want to change how you're assigning values into the array $arr, as you're pushing $min onto the array, and not $val, and if $val is less than $min, then you're changing the value of $min carrying forward to that of $val (until a lower value of $val is found in a subsequent iteration of the loop).

share|improve this answer
    
Thank for your reply. 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 –  prasenjit Nov 8 '11 at 9:10
2  
you'll have to use a 3rd party command like bc or something to do it, as bash cannot innately handle floats. or you could use Perl to write your script. –  Tim Kennedy Nov 8 '11 at 17:15
add comment
#!/bin/bash
# define as first and second parameter for script:
N=$1
n=$2
# declare as array:
declare -A arr 
k=0
min=7 # some garbage value

# for-loop with variable: Use 'seq'
for i in $(seq 1 $N)
# do...done - not curly braces:
do  
   for j in $(seq 1 $n)
   do 
     # already explained
     val=$(./a.out)
     # arithmetic evaluation in double parens:
     if (( val < min ))
     then
       min=$val
     fi
   done
   arr[$k]=$min
   k=$((k+1))
done 
array=${arr[*]}
addition=${array// /+}
echo $((addition))

Thank's to Gilles for the helpful hint in the comment.

share|improve this answer
2  
Just from a quick glance, ${arr[$k]}=$min should be arr[$k]=$min. Or are you trying to assign to a variable whose name is stored in the array? –  Gilles Nov 7 '11 at 23:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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