UNIX:如何以文件作为输入运行程序 [英] UNIX: How to run a program with a file as an input

查看:31
本文介绍了UNIX:如何以文件作为输入运行程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个名为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

例如,程序 'run' 将运行 '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 中有输入,我知道输出应该是什么样子,但是当我 cat 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 包含多行文件,每行都有一个程序检查"的输入.

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.txtasdf2.txtasdf3.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屋!

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