how to compare the runtime input with text file

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












I want to compare the input and text file word.



The text file has:



one 
two
three


Run time input assigned to a variable var:



read -p " enter the value : " var
while read first
do
a=$first
if [ "$a" == "$var" ]
then
echo " $var is found "
else
echo " $var is not found "
read -p " please enter correct value " $var
fi
done < word.txt


I tried above code in my script, but it is not working.







share|improve this question





















  • What do you want the script to do? Currently, the user has to guess the data of the first line. If they fail, they have to guess the data on the second line. If the succeed, the next line is read, and if that's not correct, the user has to guess the data on the third line. Could you please describe what the script is supposed to do?
    – Kusalananda
    Jul 30 at 7:07










  • That's is correct only. but this code is not working
    – Kannan M
    Jul 30 at 7:23










  • it should check both text file word and user input . if it is not match, it will ask for correct input from user.
    – Kannan M
    Jul 30 at 7:26










  • why $var in read command ? read -r " please enter correct value " $var
    – Kamaraj
    Jul 30 at 8:04










  • mistakenly i mentioned -r..it is changed now..if the $var value is wrong.. it will ask for new value.and again it will check the value
    – Kannan M
    Jul 30 at 9:38
















up vote
0
down vote

favorite












I want to compare the input and text file word.



The text file has:



one 
two
three


Run time input assigned to a variable var:



read -p " enter the value : " var
while read first
do
a=$first
if [ "$a" == "$var" ]
then
echo " $var is found "
else
echo " $var is not found "
read -p " please enter correct value " $var
fi
done < word.txt


I tried above code in my script, but it is not working.







share|improve this question





















  • What do you want the script to do? Currently, the user has to guess the data of the first line. If they fail, they have to guess the data on the second line. If the succeed, the next line is read, and if that's not correct, the user has to guess the data on the third line. Could you please describe what the script is supposed to do?
    – Kusalananda
    Jul 30 at 7:07










  • That's is correct only. but this code is not working
    – Kannan M
    Jul 30 at 7:23










  • it should check both text file word and user input . if it is not match, it will ask for correct input from user.
    – Kannan M
    Jul 30 at 7:26










  • why $var in read command ? read -r " please enter correct value " $var
    – Kamaraj
    Jul 30 at 8:04










  • mistakenly i mentioned -r..it is changed now..if the $var value is wrong.. it will ask for new value.and again it will check the value
    – Kannan M
    Jul 30 at 9:38












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want to compare the input and text file word.



The text file has:



one 
two
three


Run time input assigned to a variable var:



read -p " enter the value : " var
while read first
do
a=$first
if [ "$a" == "$var" ]
then
echo " $var is found "
else
echo " $var is not found "
read -p " please enter correct value " $var
fi
done < word.txt


I tried above code in my script, but it is not working.







share|improve this question













I want to compare the input and text file word.



The text file has:



one 
two
three


Run time input assigned to a variable var:



read -p " enter the value : " var
while read first
do
a=$first
if [ "$a" == "$var" ]
then
echo " $var is found "
else
echo " $var is not found "
read -p " please enter correct value " $var
fi
done < word.txt


I tried above code in my script, but it is not working.









share|improve this question












share|improve this question




share|improve this question








edited Jul 30 at 9:36
























asked Jul 30 at 6:47









Kannan M

102




102











  • What do you want the script to do? Currently, the user has to guess the data of the first line. If they fail, they have to guess the data on the second line. If the succeed, the next line is read, and if that's not correct, the user has to guess the data on the third line. Could you please describe what the script is supposed to do?
    – Kusalananda
    Jul 30 at 7:07










  • That's is correct only. but this code is not working
    – Kannan M
    Jul 30 at 7:23










  • it should check both text file word and user input . if it is not match, it will ask for correct input from user.
    – Kannan M
    Jul 30 at 7:26










  • why $var in read command ? read -r " please enter correct value " $var
    – Kamaraj
    Jul 30 at 8:04










  • mistakenly i mentioned -r..it is changed now..if the $var value is wrong.. it will ask for new value.and again it will check the value
    – Kannan M
    Jul 30 at 9:38
















  • What do you want the script to do? Currently, the user has to guess the data of the first line. If they fail, they have to guess the data on the second line. If the succeed, the next line is read, and if that's not correct, the user has to guess the data on the third line. Could you please describe what the script is supposed to do?
    – Kusalananda
    Jul 30 at 7:07










  • That's is correct only. but this code is not working
    – Kannan M
    Jul 30 at 7:23










  • it should check both text file word and user input . if it is not match, it will ask for correct input from user.
    – Kannan M
    Jul 30 at 7:26










  • why $var in read command ? read -r " please enter correct value " $var
    – Kamaraj
    Jul 30 at 8:04










  • mistakenly i mentioned -r..it is changed now..if the $var value is wrong.. it will ask for new value.and again it will check the value
    – Kannan M
    Jul 30 at 9:38















What do you want the script to do? Currently, the user has to guess the data of the first line. If they fail, they have to guess the data on the second line. If the succeed, the next line is read, and if that's not correct, the user has to guess the data on the third line. Could you please describe what the script is supposed to do?
– Kusalananda
Jul 30 at 7:07




What do you want the script to do? Currently, the user has to guess the data of the first line. If they fail, they have to guess the data on the second line. If the succeed, the next line is read, and if that's not correct, the user has to guess the data on the third line. Could you please describe what the script is supposed to do?
– Kusalananda
Jul 30 at 7:07












That's is correct only. but this code is not working
– Kannan M
Jul 30 at 7:23




That's is correct only. but this code is not working
– Kannan M
Jul 30 at 7:23












it should check both text file word and user input . if it is not match, it will ask for correct input from user.
– Kannan M
Jul 30 at 7:26




it should check both text file word and user input . if it is not match, it will ask for correct input from user.
– Kannan M
Jul 30 at 7:26












why $var in read command ? read -r " please enter correct value " $var
– Kamaraj
Jul 30 at 8:04




why $var in read command ? read -r " please enter correct value " $var
– Kamaraj
Jul 30 at 8:04












mistakenly i mentioned -r..it is changed now..if the $var value is wrong.. it will ask for new value.and again it will check the value
– Kannan M
Jul 30 at 9:38




mistakenly i mentioned -r..it is changed now..if the $var value is wrong.. it will ask for new value.and again it will check the value
– Kannan M
Jul 30 at 9:38










1 Answer
1






active

oldest

votes

















up vote
0
down vote













I'm not entirely sure what the flow should look like in your code, so I'll give two versions:



  1. The user guesses until they correctly guess the current word in the file, then moves on to guess the second word, and so on.


  2. The user guesses the current word, and moves on to the next regardless of whether the guess was correct or not.


The first variation:



exec 3<&1
n=0
while read word; do
n=$(( n + 1 ))
while true; do
printf 'Word #%dn' "$n"
read -p 'guess the word: ' -u 3 guess
if [ "$guess" = "$word" ]; then
echo 'correct'
break
fi
echo 'wrong, try again'
done
done <words
exec 3<&-


We can't just read inside the loop as that would read from the file. Instead we read from filedescriptor 3, which is a copy of standard input made before the loop. After the loop, this filedescriptor is closed.



The inner while loop iterates until a correct guess is made.



The second variation:



exec 3<&1
n=0
while read word; do
n=$(( n + 1 ))
printf 'Word #%dn' "$n"
read -p 'guess the word: ' -u 3 guess
if [ "$guess" = "$word" ]; then
echo 'correct'
else
echo 'wrong, next word...'
fi
done <words
exec 3<&-


This is similar to the previous code, but without the inner while loop.






share|improve this answer





















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f459261%2fhow-to-compare-the-runtime-input-with-text-file%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    I'm not entirely sure what the flow should look like in your code, so I'll give two versions:



    1. The user guesses until they correctly guess the current word in the file, then moves on to guess the second word, and so on.


    2. The user guesses the current word, and moves on to the next regardless of whether the guess was correct or not.


    The first variation:



    exec 3<&1
    n=0
    while read word; do
    n=$(( n + 1 ))
    while true; do
    printf 'Word #%dn' "$n"
    read -p 'guess the word: ' -u 3 guess
    if [ "$guess" = "$word" ]; then
    echo 'correct'
    break
    fi
    echo 'wrong, try again'
    done
    done <words
    exec 3<&-


    We can't just read inside the loop as that would read from the file. Instead we read from filedescriptor 3, which is a copy of standard input made before the loop. After the loop, this filedescriptor is closed.



    The inner while loop iterates until a correct guess is made.



    The second variation:



    exec 3<&1
    n=0
    while read word; do
    n=$(( n + 1 ))
    printf 'Word #%dn' "$n"
    read -p 'guess the word: ' -u 3 guess
    if [ "$guess" = "$word" ]; then
    echo 'correct'
    else
    echo 'wrong, next word...'
    fi
    done <words
    exec 3<&-


    This is similar to the previous code, but without the inner while loop.






    share|improve this answer

























      up vote
      0
      down vote













      I'm not entirely sure what the flow should look like in your code, so I'll give two versions:



      1. The user guesses until they correctly guess the current word in the file, then moves on to guess the second word, and so on.


      2. The user guesses the current word, and moves on to the next regardless of whether the guess was correct or not.


      The first variation:



      exec 3<&1
      n=0
      while read word; do
      n=$(( n + 1 ))
      while true; do
      printf 'Word #%dn' "$n"
      read -p 'guess the word: ' -u 3 guess
      if [ "$guess" = "$word" ]; then
      echo 'correct'
      break
      fi
      echo 'wrong, try again'
      done
      done <words
      exec 3<&-


      We can't just read inside the loop as that would read from the file. Instead we read from filedescriptor 3, which is a copy of standard input made before the loop. After the loop, this filedescriptor is closed.



      The inner while loop iterates until a correct guess is made.



      The second variation:



      exec 3<&1
      n=0
      while read word; do
      n=$(( n + 1 ))
      printf 'Word #%dn' "$n"
      read -p 'guess the word: ' -u 3 guess
      if [ "$guess" = "$word" ]; then
      echo 'correct'
      else
      echo 'wrong, next word...'
      fi
      done <words
      exec 3<&-


      This is similar to the previous code, but without the inner while loop.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        I'm not entirely sure what the flow should look like in your code, so I'll give two versions:



        1. The user guesses until they correctly guess the current word in the file, then moves on to guess the second word, and so on.


        2. The user guesses the current word, and moves on to the next regardless of whether the guess was correct or not.


        The first variation:



        exec 3<&1
        n=0
        while read word; do
        n=$(( n + 1 ))
        while true; do
        printf 'Word #%dn' "$n"
        read -p 'guess the word: ' -u 3 guess
        if [ "$guess" = "$word" ]; then
        echo 'correct'
        break
        fi
        echo 'wrong, try again'
        done
        done <words
        exec 3<&-


        We can't just read inside the loop as that would read from the file. Instead we read from filedescriptor 3, which is a copy of standard input made before the loop. After the loop, this filedescriptor is closed.



        The inner while loop iterates until a correct guess is made.



        The second variation:



        exec 3<&1
        n=0
        while read word; do
        n=$(( n + 1 ))
        printf 'Word #%dn' "$n"
        read -p 'guess the word: ' -u 3 guess
        if [ "$guess" = "$word" ]; then
        echo 'correct'
        else
        echo 'wrong, next word...'
        fi
        done <words
        exec 3<&-


        This is similar to the previous code, but without the inner while loop.






        share|improve this answer













        I'm not entirely sure what the flow should look like in your code, so I'll give two versions:



        1. The user guesses until they correctly guess the current word in the file, then moves on to guess the second word, and so on.


        2. The user guesses the current word, and moves on to the next regardless of whether the guess was correct or not.


        The first variation:



        exec 3<&1
        n=0
        while read word; do
        n=$(( n + 1 ))
        while true; do
        printf 'Word #%dn' "$n"
        read -p 'guess the word: ' -u 3 guess
        if [ "$guess" = "$word" ]; then
        echo 'correct'
        break
        fi
        echo 'wrong, try again'
        done
        done <words
        exec 3<&-


        We can't just read inside the loop as that would read from the file. Instead we read from filedescriptor 3, which is a copy of standard input made before the loop. After the loop, this filedescriptor is closed.



        The inner while loop iterates until a correct guess is made.



        The second variation:



        exec 3<&1
        n=0
        while read word; do
        n=$(( n + 1 ))
        printf 'Word #%dn' "$n"
        read -p 'guess the word: ' -u 3 guess
        if [ "$guess" = "$word" ]; then
        echo 'correct'
        else
        echo 'wrong, next word...'
        fi
        done <words
        exec 3<&-


        This is similar to the previous code, but without the inner while loop.







        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered 2 days ago









        Kusalananda

        101k13199311




        101k13199311






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f459261%2fhow-to-compare-the-runtime-input-with-text-file%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)