bash ssh 命令输出保存在 shell 脚本中的文本文件中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26249945/
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
ssh command output to save in a text file in shell script
提问by Mars
I want to write shell script, in which i am using ssh command. Whatever output i will get through ssh command i want save this in text file or varibale, so i can use this in my shell script. Currently i am saving output in a variable , but when i used that variable outside ssh command , value is showing blank. Code is
我想编写 shell 脚本,我在其中使用 ssh 命令。无论我将通过 ssh 命令获得什么输出,我都想将其保存在文本文件或变量中,以便我可以在我的 shell 脚本中使用它。目前我将输出保存在一个变量中,但是当我在 ssh 命令之外使用该变量时,值显示为空白。代码是
ssh hostname -c "'
`pwd`;
var=$(ps -ef | grep Consumer | cut -f6 -d' ')
'";
echo $?;
echo "vbar $var";
var value is blank when i print.
当我打印时,var 值为空。
回答by Cyrus
To save ssh's output in local file "file.log":
将 ssh 的输出保存在本地文件“file.log”中:
ssh hostname > file.log << EOF
pwd
ps -ef | grep Consumer | cut -f6 -d' '
EOF