我正在尝试在 shell 中创建一个搜索程序作为练习,但是当我尝试使用 if 语句处理空行时,我收到一条消息,指出 shell 遇到了意外的运算符。
#!/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
match_token animals_blanks dog
当我使用我运行程序时
./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
Animals_blanks 文件包含:
cat meow kitten
dog ruff pup
dog bark
cow moo calf
lion roar cub
bear roar cub
wolf howl pup