grep이 결과를 반환하지 않으면 가장 많이 투표 한 답변이 실패합니다.
Homer Simpson
Marge Simpson
Bart Simpson
Lisa Simpson
Ned Flanders
Rod Flanders
Todd Flanders
Moe Szyslak
이다 잘못 그것을 할 방법 :
wiggums=$(grep -iF "Wiggum" characters.txt);
num_wiggums=$(echo "$wiggums" | wc -l);
echo "There are ${num_wiggums} here!";
목록에 1 Wiggum 이 없다고 말할 것 입니다.
대신 변수가 비어 있는지 확인하기 위해 한 번의 추가 검사를 수행해야합니다 ( -z, "is 0"에서와 같이). grep이 아무것도 반환하지 않으면 변수는 비어 있습니다.
matches=$(grep -iF "VanHouten" characters.txt);
if [ -z "$matches" ]; then
num_matches=0;
else
num_matches=$(echo "$matches" | wc -l);
fi
echo "There are ${num_matches} VanHoutens on the list";