读取多行以重定向

时间:2014-04-24 23:56:54

标签: bash scripting

目前我有一个名为testcase的文件,在该文件中第一行有5 10 15 14 10 13 18 22在第二行

我正在尝试使用bash脚本逐行获取这两个输入来测试程序。我有while循环注释,但我觉得这是正确的方向。 我也想知道是否有可能知道我是否有两个文件并且它们是相同的返回true或类似的东西因为我现在不知道如果[["$youranswer" == "$correctanswer"]]按照我想要的方式工作。我想检查文件中的两个内容是否相同然后执行某个命令

#while read -r line
#do
#       args+=$"line"
#done < "$file_input"

# Read contents in the file
contents=$(< "$file_input")
# 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
if [ "$correctanswer" == "$youranswer" ]
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 

1 个答案:

答案 0 :(得分:1)

来自diff(1)手册页:

  

退出状态为0          如果输入相同,1如果不同,2如果有问题。

if diff -q file1 file2 &> /dev/null
  echo same
else
  echo different
fi

修改

但是,如果你坚持一次从多个文件中阅读......不要。

while read -d '\t' correctanswer youranswer
do
   ...
done < <(paste correctfile yourfile)