Unix while循环shell脚本

时间:2016-06-02 08:00:21

标签: shell unix

您好我正在尝试在unix中执行以下代码,但脚本将进入无限循环。虽然代码看起来很完美,但我无法调试错误:

answer=y
while [ "$answer" = "y" ]
do
echo " Enter the word and filename"
read pname flname
grep "$pname" "$flname" > newlist
echo " Enter any more string to be searched (y/n)? "
read anymore
case anymore in
y) answer=y ;;
n) answer=n ;;
esac
done

1 个答案:

答案 0 :(得分:3)

case之后的表达式应该是前一行读取的变量的值:

case "$anymore" in

而不是:

case anymore in

更新:

正如@Jens所提到的,你可以省略在这里对引号进行双引号,因为单词拆分不会在case (来自http://mywiki.wooledge.org/WordSplitting#line-106)内的扩展中执行。