1

Hi I am trying to execute the below code in unix but the script is going in infinite loop. Though the code looks to be perfect, i am unable to debug the error:

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
2
  • 1
    You lack the dollar sign on the variable. case $anymore pro case anymore which just examines the static string "anymore". Voting to close as simple typo. Commented Jun 2, 2016 at 8:10
  • also read without -r will mangle backslashes. Shouldn't be an issue here anyway. Commented Jun 2, 2016 at 8:26

1 Answer 1

3

The expression after case should be the value of the variable read at previous line :

case "$anymore" in

instead of :

case anymore in

Update :

As @Jens mentionned, you can omit to double-quote the variable here as word splitting is not performed on expansions inside case (from http://mywiki.wooledge.org/WordSplitting#line-106).

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks a lot Kenavoz... Its too bad from my side..i m in the learning phase of unix shell scripting...
I think you meant to say that the expression after case is not a variable. In your solution, it's a variable expansion so a value. And you may want to put $anymore in quotes to avoid errors.
Thanks Julien..as sugested by Kenavoz and you, the code is working fine with $anymore..
@JulienLopez There is no word-splitting performed on the expansion of a case expression. It is one of the few safe places to not bother with double quotes around a single parameter substitution. The other place is in variable assignments, foo=$bar.
@Jens Thanks for your comment. I did not remove the quotes here as In either case, quoting anyway will not break anything. So if in doubt, quote!.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.