1

basically when I run my code i get two syntax errors. below:

./Qscript.sh: line 34: unexpected EOF while looking for matching `"'
./Qscript.sh: line 44: syntax error: unexpected end of file

And when I fix those I still get some different errors?

#!/bin/bash
right=0
wrong=0
question=10
i=0
if [ $# -eq 2 ]
        then
for((i=0; 1<question;)) {
i=$(expr $i +1)
y=$i
echo "${awk "NR==$y" $1)"
        read -p "Enter Answer: " arg1
result=$(awk "NR==$y" $2)

if [ "$arg1" = "$result" ]
        then
right=`expr $right + 1`
else
wrong=`expr $wrong + 1`
fi
)
echo "Number_of_correct_answers " $right
echo "Number_of_wrong_answers " $wrong
fi

if [ $# -eq 3 ]
then
for((i=0; i<question;)) {
i=$(expr $i + 1)
y=$i
ans="$(awk "NR==$y" $2)"
resultfile=$(awk "NR==$y" $3)

if [ "$resultfile" = "$ans" ]
then
right=`expr $right + 1`
else
wrong=`expr $wrong + 1`
fi
)
echo $right
fi
  • 2
    There are multiple syntax errors in your script. You should really be developing incrementally rather than trying to write everything in one go. Please paste it into ShellCheck and start by correcting the first error. Then paste it in again and continue. – Kusalananda Feb 19 '17 at 17:22
  • You have 3 errors in your code, viz. on lines 11, 20, and 40. On line 11, you need $( instead of ${ whereas on lines 20 and 40 you need a } rather than ). – Rakesh Sharma Feb 19 '17 at 17:31
  • Get yourself a good editor, like "gvim" with the syntax mode enabled. That would have immediately alerted to the point of errors. – Rakesh Sharma Feb 19 '17 at 17:33
  • The for loops lack both do and done too. – Kusalananda Feb 19 '17 at 18:51
  • Bash also supports the traditional C-style for loops with braces as loop body delimiters. – Rakesh Sharma Feb 19 '17 at 19:58
0

For the sake of critique, in addition to the answers in the comments, may I suggest you try and adopt a technique of doing the following:

  • placing variables within quotes:

echo "Number_of_correct_answers ${right}"

For example, this would help avoid problems where $right could expand in certain situations with undesirable effects.

  • use `` rather than $()

Using ``, although it is not possible to nest in the same way that $( $() ) can, backticks are backwardly compatible with /bin/sh which is slightly more available than /bin/bash (such as older solaris etc).

  • don't try and nest ""

If possible, when you need to put quote in things such as awk, try using ' rather than ", if you really must, then a HEREDOC might be more appropriate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.