我有以下代码不起作用,我不知道为什么:
$ 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
答案2
关闭!
$ 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