简体   繁体   English

Bash将输出分配给此处文档中的变量

[英]Bash assign output to variable inside here document

In a bash script, what I need is to ssh to a server and do some commands, what I got so far is like this: bash脚本中,我需要的是SSH到服务器并执行一些命令,到目前为止,我得到的是这样的:

outPut="ss"
ssh user@$serverAddress << EOF
cd /opt/logs
outPut="$(ls -l)"
echo 'output is ' $outPut
EOF
echo "${outPut}"

But the output from terminal is: 但是终端的输出是:

output is  ss
ss

The output was assigned to the output from command ls -l , but what it showed is still the original value, which is ss . output已分配给命令ls -l的输出,但是显示的仍然是原始值ss What's wrong here? 怎么了

There are actually two different problems here. 实际上,这里有两个不同的问题。 First, inside a here-document, variable references and command substitutions get evaluated by the shell before the document is sent to the command. 首先,在here文档中,在将文档发送到命令之前,shell将对变量引用和命令替换进行评估。 Thus, $(ls -l) and $outPut are evaluated on the local computer before being sent (via ssh ) to the remote computer. 因此,在将$(ls -l)$outPut发送(通过ssh )到远程计算机之前,先在本地计算机上对其进行评估。 You can avoid this either by escaping the $ s (and maybe some other characters, such as escapes you want sent to the far side), or by enclosing the here-doc delimiter in quotes ( ssh user@$serverAddress << "EOF" ) which turns that feature off for the document. 您可以通过转义$ s(以及其他一些字符,例如要发送到远端的转义符),或将here-doc分隔符括在引号中( ssh user@$serverAddress << "EOF" )来避免这种情况。 ),以关闭该功能。

Second, variable assignments that happen on the remote computer stay on the remote computer. 其次,在远程计算机上发生的变量分配保留在远程计算机上。 There's no way for the local and remote shells to share state. 本地和远程Shell无法共享状态。 If you want information from the remote computer, you need to send it back as output from the remote part of the script, and capture that output (with something like remoteOutput=$(ssh ...) or ssh ... >"$tempFile" ). 如果要从远程计算机获取信息,则需要将其作为脚本的远程部分的输出发送回去,并捕获该输出(例如remoteOutput=$(ssh ...)ssh ... >"$tempFile" )。 If you're sending back multiple values, the remote part of the script will have to format its output in such a way that the local part can parse it. 如果要发回多个值,脚本的远程部分将必须格式化其输出,以使本地部分可以对其进行解析。 Also, be aware that any other output from the remote part (including error messages) will get mixed in, so you'll have to parse carefully to get just the values you wanted. 另外,请注意,来自远程部分的任何其他输出(包括错误消息)都将混入其中,因此您必须仔细解析以获取所需的值。

No, sorry that will not work. 不,对不起,该方法不起作用。

  1. Either use command substitution value (to printf) inside the here-doc (lines bettween EOF): 在here-doc中使用命令替换值(以printf表示)(EOF之间的行):

     ssh user@$serverAddress << EOF printf 'output is %s\\n' "$(ls -l /opt/logs)" EOF 
  2. Or you capture the execution of command from inside ssh, and then use the value. 或者,您可以从ssh内部捕获命令的执行,然后使用该值。

     outPut="$( ssh user@$serverAddress << EOF cd /opt/logs ls -l EOF )" echo 'output is ' "$outPut" echo "${outPut}" 

Option 1 looks cleaner. 选项1看起来更干净。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM