ファイル a.txt の最後に 2 つの空白行があります
[yaxin@oishi tmp]$ cat -n a.txt
1 jhasdfj
2
3 sdfjalskdf
4
5
私のスクリプトは次のとおりです。
[yaxin@oishi tmp]$ cat t.sh
#!/bin/sh
a=`cat a.txt`
a_length=`echo "$a" | awk 'END {print NR}'`
echo "$a"
echo $a_length
[yaxin@oishi tmp]$ sh t.sh
jhasdfj
sdfjalskdf
3
デバッグを開く
[yaxin@oishi tmp]$ sh -x t.sh
++ cat a.txt
+ a='jhasdfj
sdfjalskdf'
++ echo 'jhasdfj
sdfjalskdf'
++ awk 'END {print NR}'
+ a_length=3
+ echo 'jhasdfj
sdfjalskdf'
jhasdfj
sdfjalskdf
+ echo 3
3
cat コマンドは、ファイルの末尾にある空白行を盗みます。この問題の解決方法。