【问题标题】:Redirecting arguments into a program (BASH)将参数重定向到程序中 (BASH)
【发布时间】:2014-04-24 22:16:34
【问题描述】:

我对 bash 脚本还很陌生,我坚持的部分是 done< "$file_input" 我想要实现的是当我在测试文件中运行程序时 ./test 包含 数字 15 14 90 22 和程序将把这些数字作为参数并运行它,但我很不确定如何做到这一点。我是否朝着正确的方向前进? 感谢您的帮助

if [[ "$#" -ne 1 ]]
then
    writeusage
    exit
fi
your_path=../file/test
test_path=../../public/test
file_input="$1"
while read -r line
do
   args+="$line"
done < "$file_input"

------------------不确定如何将文本文件中的参数重定向到我的程序中,然后将结果放入名为正确答案的文件中并进行比较

# Redirect the output to a file named text
$test_path > correctanswer 2>&1
# Redirect your output to a file named text2
$your_path > youranswer 2>&1   
# diff the solutions
diff correctanswer youranswer

【问题讨论】:

标签: bash scripting piping


【解决方案1】:

您可以使用$(&lt; file) 将文件读入变量。之后不加引号使用它会导致内容作为多个参数传递。

your_path=../file/test
test_path=../../public/test
file_input="$1"
contents=$(< "$file_input")

"$test_path" $contents > correctanswer 2>&1
"$your_path" $contents > youransweranswer 2>&1

diff correctanswer youranswer

这可以使用进程替换更简洁地编写:

diff <(../../public/test $(< "$1")) <(../file/test $(< "$1"))

【讨论】:

  • 如果我在测试文件中有多行会发生什么,例如我想测试参数 5 10 15 20 和 7 8 15 14
  • 如果它在单独的行中包含5101520,则将使用四个参数51015调用该命令, 20.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-30
  • 1970-01-01
  • 2018-09-25
  • 1970-01-01
  • 2015-10-21
  • 2020-10-05
相关资源
最近更新 更多