我认为你可以这样做:
your_path=../test/test
test_path=../../public/test
file_input="$1"
while read -r -u 3 contents
do
# Display output of the test file
"$test_path" $contents > correctanswer 2>&1
# Display output of your file
"$your_path" $contents > youranswer 2>&1
# diff the solutions
diff correctanswer youranswer > /dev/null 2>&1
if [ $? -eq 0 ]
then
echo "The two outputs were exactly the same "
else
echo "$divider"
echo "The two outputs were different "
diff youranswer correctanswer
echo "Do you wish to see the ouputs side-by-side?"
select yn in "Yes" "No"; do
case $yn in
Yes) echo "LEFT: Your Output RIGHT: Solution Output"
sleep 1
vimdiff youranswer correctanswer
break;;
No) exit;;
esac
done
fi
done 3< "$file_input"
修改后read 命令读取文件描述符 3,为select 等保留标准输入。
我不知道你在做什么导致它失败。我创建了两个程序,test1 和 test2。最初,它们是相同的:
printf "%s\n" "$@"
然后我更改了test2,所以它包含:
printf "%s\n" "1$@2"
有了input.file中的这些数据:
9 11 22 13
2 35 32 16
我运行了脚本botched.sh:
$ bash -x botched.sh input.file
+ '[' -f /etc/bashrc ']'
+ . /etc/bashrc
++ '[' -z '' ']'
++ return
+ alias 'r=fc -e -'
+ your_path=./test1
+ test_path=./test2
+ file_input=input.file
+ read -r -u 3 contents
+ ./test2 9 11 22 13
+ ./test1 9 11 22 13
+ diff correctanswer youranswer
+ '[' 0 -eq 0 ']'
+ echo 'The two outputs were exactly the same '
The two outputs were exactly the same
+ read -r -u 3 contents
+ ./test2 2 35 32 16
+ ./test1 2 35 32 16
+ diff correctanswer youranswer
+ '[' 0 -eq 0 ']'
+ echo 'The two outputs were exactly the same '
The two outputs were exactly the same
+ read -r -u 3 contents
$
正如您所料,输出被识别为相同。然后使用修改后的test2,我重新运行它:
$ bash -x botched.sh input.file
+ '[' -f /etc/bashrc ']'
+ . /etc/bashrc
++ '[' -z '' ']'
++ return
+ alias 'r=fc -e -'
+ your_path=./test1
+ test_path=./test2
+ file_input=input.file
+ read -r -u 3 contents
+ ./test2 9 11 22 13
+ ./test1 9 11 22 13
+ diff correctanswer youranswer
+ '[' 1 -eq 0 ']'
+ echo ''
+ echo 'The two outputs were different '
The two outputs were different
+ diff youranswer correctanswer
1c1
< 9
---
> 19
4c4
< 13
---
> 132
+ echo 'Do you wish to see the ouputs side-by-side?'
Do you wish to see the ouputs side-by-side?
+ select yn in '"Yes"' '"No"'
1) Yes
2) No
#? 1
+ case $yn in
+ echo 'LEFT: Your Output RIGHT: Solution Output'
LEFT: Your Output RIGHT: Solution Output
+ sleep 1
+ vimdiff youranswer correctanswer
2 files to edit
+ break
+ read -r -u 3 contents
+ ./test2 2 35 32 16
+ ./test1 2 35 32 16
+ diff correctanswer youranswer
+ '[' 1 -eq 0 ']'
+ echo ''
+ echo 'The two outputs were different '
The two outputs were different
+ diff youranswer correctanswer
1c1
< 2
---
> 12
4c4
< 16
---
> 162
+ echo 'Do you wish to see the ouputs side-by-side?'
Do you wish to see the ouputs side-by-side?
+ select yn in '"Yes"' '"No"'
1) Yes
2) No
#? 2
+ case $yn in
+ exit
$
vimdiff 程序确实运行并显示了差异。
注意基本的调试技术——使用bash -x 运行。它表明读取正在使用带有四个数字的单行输入,然后将它们传递给代理程序test1 和test2;然后它需要第二行,并将它们传递给代理程序。
实际测试代码:
your_path=./test1 #../test/test
test_path=./test2 #../../public/test
file_input="$1"
while read -r -u 3 contents
do
# Display output of the test file
"$test_path" $contents > correctanswer 2>&1
# Display output of your file
"$your_path" $contents > youranswer 2>&1
# diff the solutions
diff correctanswer youranswer > /dev/null 2>&1
if [ $? -eq 0 ]
then
echo "The two outputs were exactly the same "
else
echo "$divider"
echo "The two outputs were different "
diff youranswer correctanswer
echo "Do you wish to see the ouputs side-by-side?"
select yn in "Yes" "No"; do
case $yn in
Yes) echo "LEFT: Your Output RIGHT: Solution Output"
sleep 1
vimdiff youranswer correctanswer
break;;
No) exit;;
esac
done
fi
done 3< "$file_input"
在 Mac OS X 10.9.2 上使用 Bash 3.2.51 测试。
如果它对您不起作用,那么您需要设置与我的类似的替代测试程序,并显示您在脚本上运行 bash -x 所获得的结果。您还应该确定您正在运行的平台以及您正在运行的 Bash 版本。