UNIX:如何使用文件作为输入来运行程序 [英] UNIX: How to run a program with a file as an input
问题描述
我正在编写一个名为'run'的bash脚本,用于使用预定义的输入来测试程序.
I'm writing a bash script called 'run' that tests programs with pre-defined inputs.
它将文件作为第一个参数,然后将程序作为第二个参数.
It takes in a file as the first parameter, then a program as a second parameter.
通话看起来像
./run text.txt ./check
例如,程序运行"将以text.txt作为输入运行检查".这样可以为我的程序节省大量测试时间.
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.
现在我有
$2 < text.txt > text.created
因此它将接受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.
有人知道以文件作为输入运行程序的正确方法吗?对我来说,这似乎很直观,但是检查"程序可能会出问题,而不是我在运行"脚本中正在做什么吗?
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!
文件text.txt包含多行文件,每行都具有程序"check"的输入.
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
asdf1.txt asdf2.txt asdf3.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.
推荐答案
使用
#!/bin/sh
# the whole loop reads $1 line by line
while read
do
# run $2 with the contents of the file that is in the line just read
xargs < $REPLY $2
done < $1
正常工作.将该文件称为运行",并使用
works fine. Call that file "run" and run it with
./run text.txt ./check
我得到了以text.txt
作为参数执行的程序./check
.不要忘记chmod +x run
使其可执行.
I get the program ./check
executed with text.txt
as the parameters. Don't forget to chmod +x run
to make it executable.
这是我使用的示例检查程序:
This is the sample check program that I use:
#!/bin/sh
echo "This is check with parameters $1 and $2"
将打印给定的参数.
我的文件text.txt
是:
textfile1.txt
textfile2.txt
textfile3.txt
textfile4.txt
和文件textfile1.txt
,...对于每个"check"实例每个都包含一行,例如:
and the files textfile1.txt
, ... contain one line each for every instance of "check", for example:
lets go
或
one two
输出:
$ ./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
这篇关于UNIX:如何使用文件作为输入来运行程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!