how to compare the runtime input with text file

Clash 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.
bash shell-script shell scripting
add a comment |Â
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.
bash shell-script shell scripting
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
add a comment |Â
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.
bash shell-script shell scripting
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.
bash shell-script shell scripting
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
add a comment |Â
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
add a comment |Â
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:
The user guesses until they correctly guess the current word in the file, then moves on to guess the second word, and so on.
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.
add a comment |Â
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:
The user guesses until they correctly guess the current word in the file, then moves on to guess the second word, and so on.
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.
add a comment |Â
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:
The user guesses until they correctly guess the current word in the file, then moves on to guess the second word, and so on.
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.
add a comment |Â
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:
The user guesses until they correctly guess the current word in the file, then moves on to guess the second word, and so on.
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.
I'm not entirely sure what the flow should look like in your code, so I'll give two versions:
The user guesses until they correctly guess the current word in the file, then moves on to guess the second word, and so on.
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.
answered 2 days ago
Kusalananda
101k13199311
101k13199311
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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