参数重定向到一个程序(BASH) [英] Redirecting arguments into a program (BASH)

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

问题描述

我是相当新的bash脚本编程,我被困在是后一部分
    完成< $ file_input
我试图实现的是,当我运行程序./test内testfile的含
号码15 14 90 22,程序是要采取这些数字作为论据并运行它,但我不是很确定该怎么做。我在正确的方向前进?
感谢您的帮助。

 如果[$#-ne 1]
然后
    writeusage
    出口
科幻
your_path = .. /文件/测试
test_path = .. / .. /公/测试
file_input =$ 1
而读-r线

   ARGS + =$行
完成< $ file_input

-----------------不确定如何在一个文本文件中的参数重定向到我的程序,然后把结果在一个文件名为correctanswer和他们差异比较

 #重定向输出到文件名为text
$ test_path> correctanswer 2 - ;&放大器; 1
#您重定向输出到文件名为文本2
$ your_path> youranswer 2 - ;&放大器; 1
#差异的解决方案
差异correctanswer youranswer


解决方案

您可以使用 $(小于文件)来读取一个文件到一个变量。使用它以后不带引号将导致内容为多个参数传递。

  your_path = .. /文件/测试
test_path = .. / .. /公/测试
file_input =$ 1
内容= $(小于$ file_input)$ test_path$内容> correctanswer 2 - ;&放大器; 1
$ your_path$内容> youransweranswer 2 - ;&放大器; 1差异correctanswer youranswer

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

 差异≤(../../公共/测试$(小于$ 1))≤(../文件/测试$(小于$ 1 ))

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

解决方案

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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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