从 bash 将输入发送到正在运行的 shell 脚本
我正在为我的应用程序编写一个测试套件,并使用 bash 脚本来检查测试套件输出是否与预期输出匹配。以下是脚本的一部分:
for filename in test/*.bcs ;
do
./BCSC $filename > /dev/null
NUMBER=`echo "$filename" | awk -F"[./]" '{print $2}'`
gcc -g -m32 -mstackrealign runtime.c $filename.s -o test/e$NUMBER
# run the file and diff against expected output
echo "Running test file... "$filename
test/e$NUMBER > test/e$NUMBER.out
if [ $NUMBER = "4" ]
then
# it's trying to read the line
# Pass some input to the file...
fi
diff test/e$NUMBER.out test/o$NUMBER.out
done
测试 #4 测试从 stdin 读取输入。我想测试脚本 #4,如果是的话,向它传递一组示例输入。
那样做
test/e4 < test/e4.in > test/e4.out
我刚刚意识到你可以像e4.in 具有示例输入的地方 。是否有另一种方法将输入传递给正在运行的脚本?
I'm writing a test suite for my app and using a bash script to check that the test suite output matches the expected output. Here is a section of the script:
for filename in test/*.bcs ;
do
./BCSC $filename > /dev/null
NUMBER=`echo "$filename" | awk -F"[./]" '{print $2}'`
gcc -g -m32 -mstackrealign runtime.c $filename.s -o test/e$NUMBER
# run the file and diff against expected output
echo "Running test file... "$filename
test/e$NUMBER > test/e$NUMBER.out
if [ $NUMBER = "4" ]
then
# it's trying to read the line
# Pass some input to the file...
fi
diff test/e$NUMBER.out test/o$NUMBER.out
done
Test #4 tests reading input from stdin. I'd like to test for script #4, and if so pass it a set of sample inputs.
I just realized you could do it like
test/e4 < test/e4.in > test/e4.out
where e4.in has the sample inputs. Is there another way to pass input to a running script?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想直接在脚本中提供输入数据,请使用此处文档:
有多种变体:如果您引用分隔符(即
<<'END_DATA'
),则不会'不要在此处文档中执行诸如替换 $variable 之类的操作。如果您使用<<-DELIMITER
,它将从每行输入中删除前导制表符(以便您可以缩进输入以匹配周围的代码)。有关详细信息,请参阅 bash 手册页中的“此处文档”部分。If you want to supply the input data directly in the script, use a here-document:
There are several variants: if you quote the delimiter (i.e.
<<'END_DATA'
), it won't do things like replace $variable replacement in the here-document. If you use<<-DELIMITER
, it'll remove leading tab characters from each line of input (so you can indent the input to match the surrounding code). See the "Here Documents" section in the bash man page for details.您提到的方式是发出命令/脚本时将文件重定向到标准输入的传统方法。
如果您详细说明您正在寻找的“其他方式”,也许会有所帮助,例如,为什么您甚至需要一种不同的方式来做到这一点?您是否需要执行此方法不允许执行的任何操作?
The way you mentioned is the conventional method to redirect a file into stdin when issuing a command/script.
Maybe it'll help if you'll elaborate on the "other way" you're looking for, as in, why do you even need a different way to do so? Is there anything you need to do which this method does not allow?
你可以这样做:
You can do: