Score:0

How to compare two files interactively using sed and looping

gs flag

We want to compare file1 (questions) to file2 (answers).

File1:

1
2
3

File2:

1
2
3

Ask user: does "1" (from file1 line1) equal "1" (from file2 line1) ?

User types YES or NO and gets the answer right if it was correct. If correct then add +1 to correct answers and then display amount of correct answers.

We are looking to do this for up to 10 questions/answers...

Here is an example of where we are at so far:

#!/bin/bash
clear

#constant
score=0
file1=file1.txt
file2=file2.txt

#welcome message
echo
echo
echo "Welcome to Lab 4.2 Script Quiz!"
echo "Here you will be asked questions and must provide the correct answer..."
echo
echo

# loop

i=0
while [[ i -le 10 ]]
do

#parse files for question and answer
question='sed -n $i{p} $file1'
answer='sed -n $i{p} $file2'

#print question and answer to user
echo "Is $question"
echo "The same as $answer?"

#read user choice

read -p "- Your response: (YES or NO) " user_choice

#compare user choice against solution and increment score if correct

if [ "$user_choice" == [ "$question" == "$answer" ]
then
    score=$(( ++score ))
    echo "Correct answer, you have $score correct so far"
    i=++i
else
    echo "Wrong answer, correct answer is $answer"
    i=++i
fi

done

echo "your score is: $score out of 10 correct"

We are using an existing script ( https://github.com/h4k1m0u/bash-quiz/blob/master/bash-quiz.sh ) and trying to modify it to our needs but are stuck...

We need the sed command to echo out a result for our user to compare but it just echos the full command

After the sed command echo out a result we need an if loop to check user response to YES or NO and to compare if the user response is correct...

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.