英文:
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 << EOF
sshpass -p password ssh -T root@serverIP2 << 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 << EOF
sshpass -p password ssh -T root@serverIP2 << EOS
bash /opt/Shellscriptlocation/DiskSpace.sh
EOS
EOF
)
echo "$copyoutput"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论