初学者在这里忍受我。我试图比较解决方案文件和学生提交文件中的家庭作业提交。每个文件的内容都有三个问题,每行一个:
problem 1 code
problem 2 code
problem 3 code
我想将解决方案中的每一行与学生提交中的相应行进行比较。我正在使用for循环遍历每个学生文件和嵌套的while循环来运行解决方案文件和学生文件的每一行。由于某种原因,脚本完全忽略了while循环。我在每一行之间放置了回声以查看问题所在(echo $ solution和echo $ submission只是为了检查路径是否正确):
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
当我跑步时,我得到:
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
答案 0 :(得分:1)
它不会忽略while循环 - 您正在重定向用于stdout和stderr 的文件描述符,因此echo
无法写入其中的控制台。
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
最直接的变化是我们重定向 FD3和FD4,而不是FD1和FD2 。