シェルスクリプトにループがあるパイプ

シェルスクリプトにループがあるパイプ

次の情報を含むテキストファイルがあります。

Game Copies_Sold(M) Release_date Genre   
God_of_War 19 20/04/2018 Action-Adventure 
Uncharted_4 16 10/05/2016 Action-adventure 
Spider-Man 13 07/09/2018 Action-adventure  
The_Witcher_3 10 18/05/2015 Action_role-playing

2番目の列の数字を合計する必要があるため、次のスクリプトを作成しました。

#!/bin/bash                                           
file=$1                                                   
s=0                                                      
tail -n +2 $file |cut -f 2 |                                
{ while read line                                          
do                                                      
s=$(( $s+$line ))                                        
done <$file }                                            
echo $s

しかし、明らかに私は何か間違っています。ここで何をすべきですか?ありがとうございます!

ベストアンサー1

次のようにする必要があります。

#! /bin/sh -
file=${1?}
awk 'NR >= 2 {sum += $2}; END {print sum+0}' < "$file"

アプローチに問題があります。

これらのエラーのいくつかは次のとおりです。住宅検査(システムにスタンドアロンソフトウェアとしてインストールすることもできます)。

これには次のものがあります。

$ shellcheck myscript
 
Line 4:
tail -n +2 $file |cut -f 2 |                                
           ^-- SC2086 (info): Double quote to prevent globbing and word splitting.

Did you mean: (apply this, apply all SC2086)
tail -n +2 "$file" |cut -f 2 |                                
 
Line 5:
{ while read line                                          
        ^-- SC2162 (info): read without -r will mangle backslashes.
 
Line 7:
s=$(( $s+$line ))                                        
^-- SC2030 (info): Modification of s is local (to subshell caused by pipeline).
      ^-- SC2004 (style): $/${} is unnecessary on arithmetic variables.
         ^-- SC2004 (style): $/${} is unnecessary on arithmetic variables.
 
Line 8:
done <$file }                                            
      ^-- SC2086 (info): Double quote to prevent globbing and word splitting.

Did you mean: (apply this, apply all SC2086)
done <"$file" }                                            
 
Line 9:
echo $s
     ^-- SC2031 (info): s was modified in a subshell. That change might be lost.

おすすめ記事