0

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

2
  • 1
    if [[ $question = $answer ]]
    – user986805
    Commented Feb 20, 2022 at 16:14
  • thanks for the input bac0n Commented Feb 23, 2022 at 17:21

1 Answer 1

2

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.

1
  • thank you . this is what we was after Commented Feb 20, 2022 at 16:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .