Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am writing a script where I store a predetermined line of text into a string (we will call abc). I am storing the results from grep -iqs $USER file (we will call def) into another string.

How can I test if $abc matches content in $def and if it does, move onto next function? If it does not, prompt user for input.

This is my attempt thus far:

firstFunction() {
abc="$USER content";               #text stored as string
def=`grep -iqs $USER /file`;       #results of grep stored to string
if [[ $def = "$abc" ]]; then       #test if result from grep command matches text in
   secondFunction                  #string $def. If true, move to secondFunction. If
else                               #false, prompt user.
   echo "Message to user with a y/n prompt"; read prmt;
fi;
if [[ $prmt != "y" || $prmt != "n" ]]; then prmt
elif [[ $prmt == "y" ]]; then
   sed -i "s/$def/$abc/g" /file    #If user chooses y at prompt, replace $abc with $def
elif [[ $answer == "n" ]]; then    #in file. If user chooses no at prompt, move to 
   secondFunction                  #secondFunction.
fi;
clear;
};

Environment titles have been changed but the concept behind the snippet should be clear.

share|improve this question
    
Do you still need help with this? Or have you solved it already? –  Gerhard Burger Jan 11 '13 at 12:59
    
I have solved this particular issue using case....esac –  Kevin Wyman Feb 26 '13 at 6:08
1  
please add your own solution as answer, that way everyone can benefit ;) –  Gerhard Burger Feb 26 '13 at 7:48
    
Well as mentioned, I solved that one particular issue...which is only part of a larger project overall. I found another method where I will be trying to parse options rather than create menus. I will post a link to the source for it when I finish. –  Kevin Wyman Mar 7 '13 at 8:00
    
Oke nice! if I remember correctly the book 'learning the bash shell' also has some examples on how to do this... –  Gerhard Burger Mar 7 '13 at 8:05
add comment

1 Answer

First of all, because you used grep with -q argument, the value of def variable will always be equal with an empty string. From man grep:

-q, --quiet, --silent
       Quiet;  do  not  write  anything  to  standard   output.    Exit
       immediately  with  zero status if any match is found, even if an
       error was detected.  Also see the -s  or  --no-messages  option.
       (-q is specified by POSIX.)

Second, you should think a little bit logic. This expression:

$prmt != "y" || $prmt != "n"

is always in any programming language evaluated as true:

  • if $prmt = "y", then for sure $prmt != "n", so your expression is true
  • if $prmt = "n", then for sure $prmt != "y", so your expression is true
  • in any other cases is clear that your expression is true.

Third, your function should look like:

firstFunction() {
    abc="$USER content"
    def=`grep -is $USER /path/to/file`
    if [[ $def = "$abc" ]]; then
       secondFunction 
       return                            #does not make sense to go ahead
    else
       echo "Message to user with a y/n prompt"
       read prmt
    fi
    while : ; do
    if [  "$prmt" = "n" ]; then 
       secondFunction
       return                            #does not make sense to go ahead
    elif [ "$prmt" = "y" ]; then
       sed -i "s/$def/$abc/g" /path/to/file
       return                            #does not make sense to go ahead
    else
       echo "Message to user with a y/n prompt"
       read prmt
    fi
    done
}

Or, as you said, you can use case statement.

See also:

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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