平均不同的结果,BASH

时间:2013-10-17 19:24:09

标签: bash average

此代码旨在为我提供我想要的基因中输入的最大值,最小值和平均值。

所以,一个例子:“./ script.sh 7 8 9 -g TAGLN”这段代码将给出基因TAGLN列7 8 9的最大值,最小值和平均值。

这里的问题是函数output2()的平均值给出了错误的结果......但是,函数output1()的平均值很好,我无法理解它......

#!/bin/bash

output1() {
    var1=$(cat affy.txt | cut -d"!" -f$i | sort -nr | head -1)
    echo "Maximum value of array ARRY$(($i-5|bc))X is $var1"
    var2=$(cat affy.txt | cut -d"!" -f$i | sort -nr | tail -1)
    echo "Minimum value of array ARRY$(($i-5|bc))X is $var2"
    var3=$(cat affy.txt | cut -d"!" -f$i | awk '{total += $1; count++ } END {print total/count}')
    echo -e "Average of array ARRY$(($i-5|bc))X is $var3 \n"
}

output2() {
    var1=$(grep $word affy.txt | cut -d"!" -f$i | sort -nr | head -1)
    echo "Maximum value of array ARRY$(($i-5|bc))X in gene $word is $var1"
    var2=$(grep $word affy.txt | cut -d"!" -f$i | sort -nr | tail -1)
    echo "Minimum value of array ARRY$(($i-5|bc))X in gene $word is $var2"
    var3=$(grep $word affy.txt | cut -d"!" -f$i | awk '{total += $1; count++ } END {print total/count}')
    echo -e "Average of array ARRY$(($i-5|bc))X in gene $word is $var3 \n"
}

function3() {
columns=""
for i in $@; do
    if [ $i = "-g" ]; then
        word=$2
        is_there_g="True"
        break
    else
        is_there_g="False"
        fi
        columns+="$i "
        shift
done
length="${#columns}"
echo $columns
}

deciding_the_output() {
function3 $@
if [ $# = 0 ]; then
    for i in `seq 5 20`; do
        output1 $i
    done
else
    if [ $is_there_g = "False" ]; then
    for i in $columns; do
        output1 $i
    done
    else
    if [ $length = 0 ]; then
        for i in `seq 5 20`; do
            output2 $i
        done
    else
        for i in $columns; do
            output2 $i
        done
    fi
    fi
fi
}

deciding_the_output $@

1 个答案:

答案 0 :(得分:0)

如果我输入./script.sh 8,我正在查找第8列的最大值,最小值和平均值,并且结果是正确的:

Maximum value of array ARRY3X is 9,22055
Minimum value of array ARRY3X is -8,72615
Average of array ARRY3X is 0.441644 

如果我输入./script.sh 8 -g TAGLN,我正在查找基因TAGLN的第8列的最大值,最小值和平均值,但是给出的平均值是错误的:

Maximum value of array ARRY3X in gene TAG is 1,91835
Minimum value of array ARRY3X in gene TAG is 1,68215
Average of array ARRY3X in gene TAG is 1 

它总是给出整数,如1,0 ......