在bash中读取每一行作为参数 [英] Reading each line as argument in bash

查看:20
本文介绍了在bash中读取每一行作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力学习抨击.我正在尝试运行 ./test 测试用例测试用例中的文件在下一行有参数 9 11 22 13、32 35 32 16 等等我的程序需要 4 个参数.现在,如果测试用例有一行参数,即 3 5 6 7 它可以正常工作,但是当有超过 2 个时它不能正常运行程序.我知道我需要一个while循环来读取文件的每一行,但我被困在这里.如果有人可以帮助我,将不胜感激.先感谢您.之前在这里问过这个问题,不知道大家是不是太忙了,还是不知道怎么做.再次感谢

I am trying to learn bashing. I am trying to run ./test testcase the file in test case has the arguments 9 11 22 13, 32 35 32 16 on the next line and so on My program takes 4 arguments. Right now if testcase has one line of arguments i.e 3 5 6 7 it works fine, but when there is more than 2 it can't run the program properly. I know I need a while loop to read each line of the file, but I am stuck here. If someone could help me it would be greatly appreciated. Thank you in advance. I've asked this question here before, I don't know if everyone is too busy or don't know how. Thanks again

your_path=../test/test
test_path=../../public/test    
file_input="$1"
#while read -r line; do
#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
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

推荐答案

我通常会使用类似于以下的构造(适用于您的示例).请注意,您需要进行的唯一更改是 cat $file |(while read l; do 然后 done) 结束循环.循环的主体正是您的代码,但 contents 被替换为 l (行读取).循环周围的括号实际上会启动另一个 shell 实例,在该实例中执行 while,并且它的 stdin 设置为从 cat 馈送.

I would normally use a construct similar to the following (applying to your example). Note that the only change your need to make is the cat $file | (while read l; do and then done) that closes the cycle. The body of the cycle is exactly your code with exception that contents is replaced with l (line read). Parenthesis around the cycle actually launch another instance of the shell where while is executed and its stdin is set to be fed from cat.

your_path=../test/test
test_path=../../public/test    
file_input="$1"

exec 3< $file_input
while read -u 3 l; do
# do your per-test diffs and outputs here
# variable $l will contain one line of $file_input
    # Display output of the test file
    "$test_path" $l > correctanswer 2>&1
    # Display output of your file
    "$your_path" $l > 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

这篇关于在bash中读取每一行作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆