IF ステートメントは、両方の変数 echo と grep では機能しません。

IF ステートメントは、両方の変数 echo と grep では機能しません。

うまくいかない次のコードがありますが、理由がわかりません。

$ cat test.sh
#Variables
list_of_items=$(ls /items)
my_item=ccc

#Check if my item is on the list
if [ echo ${list_of_items} | grep -wc ${my_item} -eq 0 ]
 then echo "This item is not on the list."
fi

$ ./test.sh
./test.sh: line 6: [: missing `]'
grep: ccc: No such file or directory
grep: 0: No such file or directory
grep: ]: No such file or directory
$ 

アイデアは、単語リスト(ls)に正しい単語が存在することを確認することです。助けてください?

よろしくお願いします。

ダニエル

ベストアンサー1

大きな打撃:

shopt -s nullglob
matches=( /items/*"$my_item"* )
if (( "${#matches[@]}" == 0 )); then
  echo "File matching $my_item not found"
fi

おすすめ記事