bash UNIX:如何以文件作为输入运行程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10692099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:21:08  来源:igfitidea点击:

UNIX: How to run a program with a file as an input

bashunixparametersfile-io

提问by tf.rz

I'm writing a bash script called 'run' that tests programs with pre-defined inputs.

我正在编写一个名为“run”的 bash 脚本,它使用预定义的输入测试程序。

It takes in a file as the first parameter, then a program as a second parameter.

它接受一个文件作为第一个参数,然后一个程序作为第二个参数。

The call would look like

电话看起来像

./run text.txt ./check

for example, the program 'run' would run 'check' with text.txt as the input. This will save me lots of testing time with my programs.

例如,程序 'run' 将运行 'check' 并以 text.txt 作为输入。这将为我的程序节省大量测试时间。

right now I have

现在我有

 < text.txt > text.created

So it takes the text.txt and redirects it as input in to the program specified, which is the second argument. Then dumps the result in text.created.

所以它接受 text.txt 并将其作为输入重定向到指定的程序,这是第二个参数。然后将结果转储到 text.created 中。

I have the input in text.txt and I know what the output should look like, but when I cat text.created, it's empty.

我在 text.txt 中有输入,我知道输出应该是什么样子,但是当我 cat text.created 时,它是空的。

Does anybody know the proper way to run a program with a file as the input? This seems intuitive to me, but could there be something wrong with the 'check' program rather than what I'm doing in the 'run' script?

有人知道以文件作为输入运行程序的正确方法吗?这对我来说似乎很直观,但是“检查”程序而不是我在“运行”脚本中所做的事情是否有问题?

Thanks! Any help is always appreciated!

谢谢!任何帮助总是不胜感激!

EDIT: the file text.txt contains multiple lines of files that each have an input for the program 'check'.

编辑:文件 text.txt 包含多行文件,每行都有一个程序“检查”的输入。

That is, text.txt could contain

也就是说,text.txt 可以包含

asdf1.txt asdf2.txt asdf3.txt

asdf1.txt asdf2.txt asdf3.txt

I want to test check with each file asdf1.txt, asdf2.txt, asdf3.txt.

我想对每个文件 asdf1.txt、asdf2.txt、asdf3.txt 进行测试检查。

采纳答案by topskip

A simple test with

一个简单的测试

#!/bin/sh

# the whole loop reads  line by line
while read
do
    # run  with the contents of the file that is in the line just read
    xargs < $REPLY 
done < 

works fine. Call that file "run" and run it with

工作正常。调用该文件“运行”并运行它

./run text.txt ./check

I get the program ./checkexecuted with text.txtas the parameters. Don't forget to chmod +x runto make it executable.

我将程序作为参数./check执行text.txt。不要忘记chmod +x run使其可执行。

This is the sample check program that I use:

这是我使用的示例检查程序:

#!/bin/sh

echo "This is check with parameters  and "

Which prints the given parameters.

打印给定的参数。

My file text.txtis:

我的文件text.txt是:

textfile1.txt
textfile2.txt
textfile3.txt
textfile4.txt

and the files textfile1.txt, ... contain one line each for every instance of "check", for example:

和文件textfile1.txt,...每个“检查”实例都包含一行,例如:

lets go

or

或者

one two

The output:

输出:

$ ./run text.txt ./check
This is check with parameters lets and go
This is check with parameters one and two
This is check with parameters uno and dos
This is check with parameters eins and zwei

回答by dmckee --- ex-moderator kitten

The <operator redirects the contents of the file to the standard inputof the program. This is not the same as using the file's contents for the arguments of the file--which seems to be what you want. For that do

<操作重定向文件的内容到标准输入的程序。这与使用文件内容作为文件参数不同——这似乎是您想要的。为此做

./program $(cat file.txt) 

in bash (or in plain old /bin/sh, use

在 bash (或在普通的 old 中/bin/sh,使用

./program `cat file.txt`

).

)。

This won't manage multiple lines as separate invocations, which your edit indicates is desired. For that you probably going to what some kind scripting language (perl, awk, python...) which makes parsing a file linewise easy.

这不会将多行作为单独的调用进行管理,您的编辑表明需要这样做。为此,您可能会使用某种脚本语言(perl、awk、python...),这使得逐行解析文件变得容易。