Suppose I have the following code in bash:
#!/bin/bash
bad_fruit=( apple banana kiwi )
for fname in `ls`
do
# want to check if fname contains one of the bad_fruit
is_bad_fruit=??? # <------- fix this line
if [ is_bad_fruit ]
then
echo "$fname contains bad fruit in its name"
else
echo "$fname is ok"
fi
done
How do I fix is_bad_fruit so that it is true if fname contains one of the bad_fruit strings?
Try the following code :
Take care of useless use of ls
lsis a tool for interactively looking at file information. Its output is formatted for humans and will cause bugs in scripts. Use globs or find instead. Understand why : http://mywiki.wooledge.org/ParsingLs