在"heredoc"内部循环在shell脚本中 [英] Loop inside "heredoc" in shell scripting
问题描述
我需要在带有参数化值的交互式程序/实用程序中执行一系列命令.有没有一种方法可以在heredoc中循环?如下所示..不确定eval
是否对您有帮助.下面的示例似乎不起作用,因为交互似乎无法识别系统命令.
I need to execute series of commands inside an interactive program/utility with parameterized values. Is there a way to loop inside heredoc ? Like below .. Not sure if eval
can be of any help here. Below example doesn't seem to work as the interactive doesn't seem to recognize system commands.
#!/bin/sh
list="OBJECT1 OBJECT2 OBJECT3"
utilityExecutable << EOF
for i in $list ; do
utilityCommand $i
done
EOF
推荐答案
不是将此处文档传递给utilityExecutable
,
等效的是将所需的文本通过管道传递给它.您可以在for循环中使用echo
语句创建所需的文本,并将整个循环输出通过管道传递到utilityExecutable
:
Instead of passing a here-document to utilityExecutable
,
the equivalent is to pipe the required text to it. You can create the desired text using echo
statements in a for-loop, and pipe the entire loop output to utilityExecutable
:
#!/bin/sh
list="OBJECT1 OBJECT2 OBJECT3"
for i in $list; do
echo "utilityCommand $i"
done | utilityExecutable
这篇关于在"heredoc"内部循环在shell脚本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!