0

Beginner here so bear with me. I am trying to compare homework submissions from a solution file and a student submission file. The contents of each file have three problems, one per line:

problem 1 code
problem 2 code
problem 3 code

I want to compare each line in the solution with the corresponding line in the students submission. I am using a for loop to run through each student file and a nested while loop to run through each line of the solution file and student file. For some reason the script is completely ignoring the while loop. I have put echoes between each line to see where the problem is(the echo $solution and echo $submission is just to check to see if the path is correct):

 for submission in /home/myfolder/submissions/*

    do
    echo 1
    solution=$(echo /home/myfolder/hwsolution/*)
    echo 2
    echo $solution
    echo $submission
            while read sans <&1 && read sol <&2
             do
    echo 3
             echo Student awnser is: $sans
             echo Solution is: $sol
    echo 4
            done 1<$(echo $submission) 2<$(echo $(echo $solution))
    echo 5
done

When I run it I get:

1
2
/home/myfolder/hwsolution/solution
/home/myfolder/submissions/student1
5
1
2
/home/myfolder/hwsolution/solution
/home/myfolder/submissions/student2
5
1
2
/home/myfolder/hwsolution/solution
/home/myfolder/submissions/student3
5
13
  • There's no nested while loop in your code, and it's not clear how the output you post relates to the output you expect or want. Commented Nov 18, 2017 at 21:02
  • Also, it's not clear what useful thing you expect 1<$(echo $submission) or 2<$(echo $(echo $solution)) to accomplish. (Also, using file descriptor 1 or file descriptor 2 for input is a really bad idea, as those two are reserved for stdout and stderr, respectively). Commented Nov 18, 2017 at 21:04
  • ...do you maybe want to read the contents of the files? If so, then you want <"$solution", with no echo. Commented Nov 18, 2017 at 21:05
  • I told you -- it's not ignoring them at all. It's trying to run the loop, but the echos inside the loop don't work because you overrode stdout and stderr. (Also, it's not iterating over the lines in the file, but iterating over the names of the files, because of your unnecessary/inappropriate echos in the process substitutions). Commented Nov 18, 2017 at 21:11
  • FD 0, FD 1 and FD 2 are reserved, so when you run 1<... and 2<..., you break any command or program inside the area subject to redirection that tries to log to stdout (FD 1) or stderr (FD 2). And because you broke the logging, you can't see any logs that tell you what you broke. Commented Nov 18, 2017 at 21:13

1 Answer 1

1

It's not ignoring the while loop -- you're redirecting the file descriptors used for stdout and stderr, so echo can't write to the console within it.

for submission in /home/myfolder/submissions/*; do
    solutions=( /home/myfolder/hwsolution/* )

    if (( ${#solutions[@]} == 1 )) && [[ -e ${solutions[0]} ]]; then
      solution=${solutions[0]}
    else
      echo "Multiple solution files found; don't know which to use" >&2
      printf ' - %q\n' "${solutions[@]}" >&2
      exit
    fi

    while read sans <&3 && read sol <&4; do
             echo "Student awnser is: $sans"
             echo "Solution is: $sol"
    done 3<"$submission" 4<"$solution"
done

The most immediate change is that we're redirecting FD3 and FD4, not FD1 and FD2.

Sign up to request clarification or add additional context in comments.

5 Comments

I'm getting "multiple solution files found..." even though there is only one file in home/myfolder/hwsolution. There will only ever be one file in there.
Added a printf to log the candidates, so you can see exactly what it thinks the list of files is, and thus figure out why/how you have something other than exactly one file.
I think it worked! I forgot to delete the $ in solutions=( /home/myfolder/hwsolution/* ). You're a life saver man. So what is this code actually doing? I don't quite understand the &3 and &4. I got that suggestion from another post but we have not learned about that in my class.
...btw, BashFAQ #1 is a good related resource, covering read in detail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.