0

这段代码是为了给我在我想要的基因中输入的列的最大值、最小值和平均值。

因此,举个例子:“./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 $@
4

1 回答 1

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

于 2013-10-17T19:47:54.540 回答