问题描述
目前我有一个名为 testcase 的文件,在该文件的第一行有 5 10 15 14 和10 13 18 22 在线二
currently I have a file named testcase and inside that file has 5 10 15 14 on line one and 10 13 18 22 on line two
我正在尝试使用 bash 脚本将这两个输入一行一行地测试到一个程序中.我把 while 循环注释掉了,但我觉得这是在朝着正确的方向发展.我还想知道是否有可能知道我是否区分了两个文件并且它们是相同的返回 true 或类似的东西 因为我现在不知道 if [["$youranswer" == "$正确答案"]]
正在按照我想要的方式工作.我想检查文件中的两个内容是否相同然后执行某个命令
I am trying to bash script to take those two inputs line by line to test into a program. I have the while loop comment out but I feel like that is going in the right direction.
I was also wondering if is possible to know if I diff two files and they are the same return true or something like that because I dont now if [["$youranswer" == "$correctanswer"]]
is working the way I wanted to. I wanted to check if two contents inside the files are the same then do a certain command
#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
推荐答案
来自 diff(1)
手册页:
退出状态为 0如果输入相同,则为 1,如果不同,则为 2.
Exit status is 0 if inputs are the same, 1 if different, 2 if trouble.
if diff -q file1 file2 &> /dev/null
echo same
else
echo different
fi
但是如果您坚持一次读取多个文件......不要.
But if you insist on reading from more than one file at a time... don't.
while read -d ' ' correctanswer youranswer
do
...
done < <(paste correctfile yourfile)
这篇关于读取多行以重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论