我正在编写一个名为“运行”的 bash 脚本,用于测试具有预定义输入的程序。
它接受一个文件作为第一个参数,然后一个程序作为第二个参数。
调用看起来像
./run text.txt ./check
例如,程序“run”将以 text.txt 作为输入运行“check”。这将为我节省大量的程序测试时间。
现在我有
$2 < text.txt > text.created
因此它获取 text.txt 并将其作为输入重定向到指定的程序,这是第二个参数。然后将结果转储到 text.created 中。
我在 text.txt 中有输入,我知道输出应该是什么样子,但是当我 cat text.created 时,它是空的。
有人知道以文件作为输入来运行程序的正确方法吗?这对我来说似乎很直观,但是“检查”程序会不会有问题,而不是我在“运行”脚本中所做的事情?
谢谢!任何帮助总是感激!
编辑:文件 text.txt 包含多行文件,每行文件都有一个程序“check”的输入。
也就是说,text.txt 可以包含
asdf1.txt asdf2.txt asdf3.txt
我想测试检查每个文件 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
工作正常。调用该文件“运行”并运行它
./run text.txt ./check
我让程序 ./check
以 text.txt
作为参数执行。不要忘记 chmod +x run
使其可执行。
这是我使用的示例检查程序:
#!/bin/sh
echo "This is check with parameters $1 and $2"
打印给定的参数。
我的文件 text.txt
是:
textfile1.txt
textfile2.txt
textfile3.txt
textfile4.txt
和文件 textfile1.txt
, ... 为每个“check”实例各包含一行,例如:
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
关于bash - UNIX:如何以文件作为输入来运行程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10692099/