3

I have tried to use a 'case' loop but it doesn't work for a reason unknown to me.

This is my script for the case loop.

echo "Do you wish to search again? [y/n]"
read INPUT_STRING2
case $INPUT_STRING2
   y)
   ;;
   *)
   exit
   ;;

Then I am having issues trying to make my script more user friendly, I want my script to tell the user if the string isn't found but I can't figure it out. I have tried using a case loop instead but have had no luck, and tried to use an else statement but it didn't work.

if grep -q $STRING $FILE ; then
             echo "string found"
fi

This part works but isn't all of what I want.

3

There seems to be an 'in' missing in the line with the case statement as well as a closing esac:

echo "Do you wish to search again? [y/n]"
read INPUT_STRING2
case $INPUT_STRING2 in
   y)
   ;;
   *)
   exit
   ;;
esac
| improve this answer | |
3

You did two syntax errors (in after the variable and esac as end of the case statement):

echo "Do you wish to search again? [y/n]"
read INPUT_STRING2
case $INPUT_STRING2 in
   "y")
   echo "Searching again"
   ;;
   *)
   exit
   ;;
esac
| improve this answer | |
1

I want my script to tell the user if the string isn’t found ….    I have tried ... to use an else statement but it didn't work.

What did you try?

if grep -q "$STRING" "$FILE"
then
        echo "string found"
else
        echo "string not found"
fi

works.

P.S. I advise you to quote all references to shell variables (e.g., "$INPUT_STRING2", "$STRING", and "$FILE") unless you have a really good reason not to and you’re sure you know what you're doing.  Conversely, you don't need to quote constant strings (e.g., string found) unless they contain special characters.  (Square brackets – “[” and “]” – are special characters in this context.)  But it’s a good idea to quote them anyway.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.