I am attempting to create a search program in shell as an exercise, but when I try to handle empty lines with an if-statement I get a message saying that the shell encountered an unexpected operator.
#!/bin/sh
file=$1
token=$2
while read line
do
if [ ! -z $line ]
then
set $line
if [ $1 = $token ]
then
echo $line
fi
fi
done < $file
When I run the program using match_token animals_blanks dog
I get
./match_token: 8: [: cat: unexpected operator
./match_token: 8: [: dog: unexpected operator
./match_token: 8: [: dog: unexpected operator
./match_token: 8: [: cow: unexpected operator
./match_token: 8: [: lion: unexpected operator
./match_token: 8: [: bear: unexpected operator
./match_token: 8: [: wolf: unexpected operator
The animals_blanks files contains:
cat meow kitten
dog ruff pup
dog bark
cow moo calf
lion roar cub
bear roar cub
wolf howl pup
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…