将参数重定向到程序(BASH)

时间:2014-04-24 22:16:34

标签: bash scripting piping

我对bash脚本很新,而且我坚持的部分就是追求     done< "$file_input" 我想要实现的是当我在testfile中包含运行程序./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"

-----------------不确定如何将文本文件中的参数重定向到我的程序中,然后将结果放在名为correctanswer的文件中并将它们区分开来

# 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

1 个答案:

答案 0 :(得分:0)

您可以使用$(< 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"))