Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Why my code isn't outputting if a string entered isn't in the file. When I enter a string and it isn't in the file, there's no response back, it re-loops back to the beginning. Can someone tell me what's wrong with my code?

while :
do
echo "Please enter a string"
read input_string
echo "Please enter the file name too see if that string is present in it - (Enter .abw after)"
read input_string1
if grep -q $input_string $input_string1 ; then
echo  "Your string has been found"
fi
done
share|improve this question

closed as unclear what you're asking by Gilles, slm Sep 23 at 23:39

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

3  
What are you expecting to happen? –  Patrick Sep 22 at 12:42
    
It was meant to say that a string which you enter, it looks for it in the file and it says if that string is present. I've now realised that I need to enter another bit of code for else. Thanks –  Adam Poyser Sep 22 at 12:46
    
input_string isn't really a speaking variable name, by the way :) –  Marian Sep 22 at 17:12
1  
You need double quote around variable expansions. Read Why does my shell script choke on whitespace or other special characters? –  Gilles Sep 23 at 23:02

2 Answers 2

while :
 do
     echo "Please enter a string"
     read input_string
     echo "Please enter the file name too see if that string is present in it - (Enter .abw after)"
     read input_string1
     grep -q "${input_string}" "${input_string1}"                                                                 
     if [ $? -eq 0 ] ; then
         echo  "Your string has been found"
     else 
         echo "Your string has not been found"
     fi
 done
share|improve this answer
    
I've just tried this code and it works thanks! I didn't realise that all my problem was with not having an else statement. Thanks for the quick response –  Adam Poyser Sep 22 at 12:53
2  
You need to quote grep parameters. Think what would happen if my search string includes -v, or there are spaces in the filename. –  Ángel Sep 22 at 19:33
    
@Ángel Thanks, done that now –  Fazle A. Sep 25 at 13:01

You figured out your missing else-branch, but one suggestion:

instead of using $input_string $input_string1 try ${input_string} ${input_string1} just to make sure you don't get $input_string followed by 1.

share|improve this answer
    
No, your proposed replacement is exactly equivalent to the original. $input_string1 is the value of the variable input_string1 (split and globbed, since it isn't quoted), it doesn't involve the variable input_string. –  Gilles Sep 23 at 23:00

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