如何将远程主机的输出存储在可变 bash 脚本中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18015327/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:06:36  来源:igfitidea点击:

How to store output from a remote host in a variable bash script

bash

提问by Ruchir Bharadwaj

I need to get all the wars on a remote tomcat server printed via my bash script. I am able to ssh on the remote server and execute the command to get wars with version :-

我需要通过我的 bash 脚本打印远程 tomcat 服务器上的所有War。我能够在远程服务器上 ssh 并执行命令以获取与版本的War:-

${SSH} ${DEST_USER}@${DESTHOST} "Command"

however if I am storing it in a variable I don't get any-thing:-

但是,如果我将它存储在一个变量中,我不会得到任何东西:-

output=${SSH} ${DEST_USER}@${DESTHOST} "Command"
echo $output

Based on a bit research if I try it as

如果我尝试将其作为基于一点研究

output=$(${SSH} ${DEST_USER}@${DESTHOST} "Command"
echo $output

I got my output stored (need to understand why?).

我存储了我的输出(需要了解为什么?)。

Secondly the output I got in a single line as:

其次,我在一行中得到的输出为:

abc_1.2.war ijk_2.2.war hij_3.2.war xyx_5.1.war

how can I format this output some thing like:

我怎样才能格式化这个输出,比如:

Name version
abc  1.2
ijk  2.2
hij  3.2
xyz  5.1  

回答by glenn Hymanman

Bash handles lines like this:

Bash 处理这样的行:

a=b c=d command

by temporarily setting environment variables "a" and "c" only for the duration of the command. They are not normal shell variables, and they won't exist after the command completes.

通过仅在命令期间临时设置环境变量“a”和“c” 。它们不是普通的 shell 变量,在命令完成后它们将不存在。

When you write

当你写

output=${SSH} ${DEST_USER}@${DESTHOST} "Command"

you are setting the "output" variable to the contents of "$SSH" for the duration of the "$DEST_USER@$DESTHOST" command (I'd expect you would receive a "command not found" error).

在“$DEST_USER@$DESTHOST”命令期间,您将“输出”变量设置为“$SSH”的内容(我希望您会收到“找不到命令”错误)。

The way to capture the output of a command, as you have discovered, is with the $()syntax

正如您所发现的,捕获命令输出的方法是使用$()语法

output=$($SSH $DEST_USER@$DESTHOST "Command")

(Note you don't always have to put braces around your variable names, that's only strictly necessary when you need to separate the variable name from surrounding text)

(请注意,您不必总是在变量名周围加上大括号,这仅在您需要将变量名与周围文本分开时才是绝对必要的)



For your text formatting question:

对于您的文本格式问题:

output="abc_1.2.war ijk_2.2.war hij_3.2.war xyx_5.1.war"
{
    echo Name version
    for word in $output; do                    # no quotes around $output here!
        IFS=_ read name version <<< "$word"
        echo "$name" "${version%.*}"
    done
} | column -t
Name  version
abc   1.2
ijk   2.2
hij   3.2
xyx   5.1

Here I use braces to group the header and the lines from the for-loop together to send them to column. I don't assume you know all the file extensions, but I do assume each file has an extension. The "${version%.*}"part removes the last dot and any following characters from the end of the value of the "version" variable.

在这里,我使用大括号将标题和 for 循环中的行组合在一起以将它们发送到列。我不假设您知道所有文件扩展名,但我假设每个文件都有一个扩展名。该"${version%.*}"部分从“版本”变量的值的末尾删除最后一个点和任何后续字符。

回答by kan

Try echo "$output"(i.e. quotes).

尝试echo "$output"(即引号)。

回答by blackSmith

I can't help with the 'why' right now. However you can produce the desired output from the variable(containing single line) as follows :

我现在无法回答“为什么”。但是,您可以从变量(包含单行)生成所需的输出,如下所示:

echo "Name   version"
echo $output | grep -Eo "[a-zA-Z]+_[0-9]+.[0-9]+"| tr '_' ' '