这里文档作为 bash 函数的参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4664229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Here document as an argument to bash function
提问by Niebelung
Is it possible to pass a here document as a bash function argument, and in the function have the parameter preserved as a multi-lined variable?
是否可以将 here 文档作为 bash 函数参数传递,并且在函数中将参数保存为多行变量?
Something along the following lines:
大致如下:
function printArgs {
echo arg1=""
echo -n arg2=
cat <<EOF
EOF
}
printArgs 17 <<EOF
18
19
EOF
or maybe:
或者可能:
printArgs 17 $(cat <<EOF
18
19
EOF)
I have a here document that I want to feed to ssh as the commands to execute, and the ssh session is called from a bash function.
我有一个 here 文档,我想将它作为要执行的命令提供给 ssh,并且 ssh 会话是从 bash 函数调用的。
采纳答案by Paused until further notice.
If you're not using something that will absorb standard input, then you will have to supply something that does it:
如果您不使用可以吸收标准输入的东西,那么您将不得不提供可以吸收标准输入的东西:
$ foo () { while read -r line; do var+=$line; done; }
$ foo <<EOF
a
b
c
EOF
回答by Robokop
The way to that would be possible is:
可能的方法是:
printArgs 17 "$(cat <<EOF
18
19
EOF
)"
But why would you want to use a heredoc for this? heredoc is treated as a file in the arguments so you have to (ab)use catto get the contents of the file, why not just do something like:
但是为什么要为此使用heredoc?heredoc 在参数中被视为文件,因此您必须 (ab)usecat来获取文件的内容,为什么不执行以下操作:
print Args 17 "18
19"
Please keep in mind that it is better to make a script on the machine you want to ssh to and run that then trying some hack like this because bash will still expand variables and such in your multiline argument.
请记住,最好在您想要通过 ssh 连接的机器上制作一个脚本并运行它,然后尝试一些像这样的 hack,因为 bash 仍然会在您的多行参数中扩展变量等。
回答by starfry
Building on Ned's answer, my solution allows the function to take its input as an argument list or as a heredoc.
基于 Ned 的回答,我的解决方案允许该函数将其输入作为参数列表或 heredoc。
printArgs() (
[[ $# -gt 0 ]] && exec <<< $*
ssh -T remotehost
)
So you can do this
所以你可以这样做
printArgs uname
or this
或这个
printArgs << EOF
uname
uptime
EOF
So you can use the first form for single commands and the long form for multiple commands.
因此,您可以对单个命令使用第一种形式,对多个命令使用长形式。
回答by Ned Deily
One way to feed commands to ssh through a here doc and a function is as so:
通过此处的文档和函数向 ssh 提供命令的一种方法如下:
#!/bin/sh
# define the function
printArgs() {
echo ""
ssh -T remotehost
}
# call it with a here document supplying its standard input
printArgs 17 <<EOF
uname
uptime
EOF
The results:
结果:
17
Linux remotehost 2.6.32-5-686 ...
Last login: ...
No mail.
Linux
16:46:50 up 4 days, 17:31, 0 users, load average: 0.06, 0.04, 0.01
回答by Jér?me Pouiller
xargsshould do exactly what you want. It convert standard input to argument for a command (notice -0allow to preserve newlines)
xargs应该做你想做的。它将标准输入转换为命令的参数(注意-0允许保留换行符)
$ xargs -0 <<EOF printArgs 17
18
19
EOF
But for you special case, I suggest you to send command on standard input of ssh:
但是对于您的特殊情况,我建议您在 ssh 的标准输入上发送命令:
$ ssh host <<EOF
ls
EOF

