Redirecting arguments into a program (BASH)

允我心安 提交于 2019-12-18 09:47:37

问题


I am fairly new to bash scripting and a part I am stuck on is after done< "$file_input" What I am trying to achieve is when I run the program ./test inside the testfile contains the numbers 15 14 90 22 and the program is going to take those numbers as the arguments and run it, but I am quite unsure how to do that. Am I going in the correct direction? Thanks for any help

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"

----------------- unsure how to redirect the arguments in a text file into my program and then put the result in a file named correctanswer and diff them

# 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:


You can use $(< file) to read a file into a variable. Using it unquoted afterwards will cause the contents to be passed as multiple arguments.

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

This can more succinctly be written using process substitution:

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


来源:https://stackoverflow.com/questions/23280857/redirecting-arguments-into-a-program-bash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!