運行我testing.sh文件排序3列,並獲得平均
#!/bin/bash
FILE=$1
COUNT=0
while read -r SID FIRST LAST S1 S2 S3
do
SUM=$(expr $S1 + $S2 + $S3)
AVG=$(expr $SUM/3)
printf '%d [%d] %s, %s\n' "$AVG" "$SID" "$LAST" "$FIRST"
done < "$FILE" | sort -k 3,3n -k 4,4n -k 2,2g
我收到以下錯誤。
expr: non-integer argument
expr: syntax error
我不知道我在做什麼錯。我也嘗試過echo,awk和bc,但是我對它們都有類似的錯誤。
我會像這樣的文件:
/testing.sh /home/user/Desktop/sample.txt
這是示例文本。
123456789 Lee Johnson 72 85 90
999999999 Jaime Smith 90 92 91
888111818 JC Forney 100 81 97
290010111 Terry Lee 100 99 100
199144454 Tracey Camp 77 84 84
299226663 Laney Camp 70 74 71
434401929 Skyler Camp 78 81 82
928441032 Jess Forester 85 80 82
928441032 Chris Forester 97 94 89
當我運行我的程序,這應該是輸出:
71 [299226663] Camp, Laney
80 [434401929] Camp, Skyler
81 [199144454] Camp, Tracey
93 [928441032] Forester, Chris
82 [928441032] Forester, Jess
92 [888111818] Forney, JC
82 [123456789] Johnson, Lee
99 [290010111] Lee, Terry
91 [999999999] Smith, Jaime
工作正常,在這裏與您的數據。你能輸出$ S1,$ S2,$ S3嗎? –
@ Jean-FrançoisFabre當我自己輸出它們時我就會得到它。 numberne 6無效:printf:最後一列中的數字爲90(每行值爲90)。 – ellsusan
如果您的任何文件已通過MS Windows環境,請使用'cat -vet myScript.sh myData.txt'確認它們具有'\ r \ n'行結尾。如果你在行尾看到'^ M $',你會有'\ r \ n'行尾。使用'dos2unix myScript.sh myData.txt'來清理它們。祝你好運。 – shellter