读取多行以重定向

编程入门 行业动态 更新时间:2024-10-26 02:32:46
本文介绍了读取多行以重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有一个名为 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:关键词]

本文发布于:2023-03-27 02:30:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/674142.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!