将Bash输出捕获到Shell脚本中的变量中

huangapple go评论64阅读模式
英文:

Capture Bash output to variable in Shell Script

问题

我已经编写了一个shell脚本来在远程服务器上运行其他shell脚本(shell脚本在远程服务器上成功运行)。我想将输出捕获到某个变量中。以下是我的代码。

请问有人可以帮我吗?

#! /bin/sh
sshpass -p password ssh -T root@serverIP1 <<EOF
sshpass -p password ssh -T root@serverIP2 <<EOS
copyoutput=bash /opt/Shellscriptlocation/DiskSpace.sh
EOS
EOF
echo $copyoutput
英文:

I have written a shell script to run other shell script on remote server(Shell script is running successfully on remote server). I want to capture output to some variable. Below is my code.

Please can anyone help me out.

#! /bin/sh
sshpass -p password ssh -T root@serverIP1 &lt;&lt; EOF
sshpass -p password ssh -T root@serverIP2 &lt;&lt; EOS
copyoutput=bash /opt/Shellscriptlocation/DiskSpace.sh
EOS
EOF
echo $copyoutput

答案1

得分: 1

这应该适用于您,通过将命令替换放在最外层的Shell中:

copyoutput=$(
sshpass -p password ssh -T root@serverIP1 <<EOF
sshpass -p password ssh -T root@serverIP2 <<EOS
bash /opt/Shellscriptlocation/DiskSpace.sh
EOS
EOF
)

echo "$copyoutput"
英文:

This should work for you by placing command substitution in outermost shell:

copyoutput=$(
sshpass -p password ssh -T root@serverIP1 &lt;&lt; EOF
sshpass -p password ssh -T root@serverIP2 &lt;&lt; EOS
bash /opt/Shellscriptlocation/DiskSpace.sh
EOS
EOF
)

echo &quot;$copyoutput&quot;

huangapple
  • 本文由 发表于 2020年1月3日 15:55:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575006.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定