1

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.

1

3 Answers 3

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

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

Assuming bash or compatible shell.

1
  • Thanks for your reply. Actually, I want to know the exact "shell script" for my pseudo-code as totally unfamiliar with shell script. Nov 7, 2011 at 4:08
3

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

2
  • 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 Nov 8, 2011 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. Nov 8, 2011 at 17:15
0
#!/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.

1
  • 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? Nov 7, 2011 at 23:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .