将参数重定向到程序

将参数重定向到程序

本文介绍了将参数重定向到程序 (BASH)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 containsthe 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

推荐答案

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

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"))

这篇关于将参数重定向到程序 (BASH)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 22:07