Last active
August 11, 2017 05:11
-
-
Save ksomemo/5844324 to your computer and use it in GitHub Desktop.
1行に数値のみを記載されたテキストの合計値を出力する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
sum=0 | |
while read line | |
do | |
sum=`expr $sum + $line` | |
done < num.txt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/bin/bash | |
cat <<EOF > num.txt | |
1 | |
2 | |
3 | |
4 | |
EOF | |
awk '{v+=$1} END {print v}' nums.txt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
shellでは数値計算をexprを使って行う。
lineが数として変換できないとエラーとなる。
空行でもエラーとなるので注意する。