Score:0

using sed command to give variables with echo

gs flag

We are trying to use the sed command to give back contents from 2 files (file1q and file1a) - a question and answer file.

The question and answer file are the same with numbers on each line:

1
2
3
4
5
6
7
8
9
10

We are trying to echo the result however the sed command is being echo'd and not the result from the sed command

This is our code:

#!/bin/bash

#clear screen
clear

#reset score to 0
score=0

#loop over files to find contents
i=1
while [ $i -le 10 ]
do

question="sed -n $i{p} file1q.txt"
answer="sed -n $i{p} file1a.txt"

if [ question == answer ]
then
    echo "Correct"
else
    echo "incorrect"
fi

i=$(( $i + 1 ))

done

As you can see i = 1 so sed should be printing line 1 from both files... However, this is what we are getting (using double quotes): output with double quotes for echo

this is what we are getting with single quotes: output with single quotes for echo

this is what we want:

output without echo

bac0n avatar
cn flag
`if [[ $question = $answer ]]`
robot010101 avatar
gs flag
thanks for the input bac0n
Score:2
cn flag

You assign to the question and answer variables command strings, not output of the commands. It looks like you want this:

question=$(sed -n $i{p} file1q.txt)
answer=$(sed -n $i{p} file1a.txt)

This will run sed commands and assign output to the vars.

robot010101 avatar
gs flag
thank you . this is what we was after
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.