Tell me more ×
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 at 12:59
I have solved this particular issue using case....esac – Kevin Wyman Feb 26 at 6:08
please add your own solution as answer, that way everyone can benefit ;) – Gerhard Burger Feb 26 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 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 at 8:05

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.