linux - ssh 命令输出以保存在 shell 脚本中的文本文件中

标签 linux bash shell unix ssh

我想编写 shell 脚本,我在其中使用 ssh 命令。 无论我将通过 ssh 命令获得什么输出,我都想将它保存在文本文件或变量中,这样我就可以在我的 shell 脚本中使用它。 目前我将输出保存在一个变量中,但是当我在 ssh 命令之外使用该变量时,值显示为空白。 代码是

ssh hostname -c "'
`pwd`;
var=$(ps -ef | grep Consumer | cut -f6 -d' ')
'";
echo $?;

echo "vbar  $var";

打印时var值是空的

最佳答案

将 ssh 的输出保存在本地文件“file.log”中:

ssh hostname > file.log << EOF
pwd
ps -ef | grep Consumer | cut -f6 -d' '
EOF

关于linux - ssh 命令输出以保存在 shell 脚本中的文本文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26249945/

相关文章:

Linux 脚本计数器无法正常工作

python - 如何在linux中执行python文件

bash - 为什么cp在一台服务器上显示 "is not a directory"而在另一台服务器上没问题

仅在 bash 中解释的 Python3 f 字符串

linux - 导出不通过另一个脚本工作

java - IOException :cannot run program:CreateProcess error=193, %1 不是有效的 Win32 应用程序

c - 查询服务是否正在运行

linux - 在 Hive 中使用不同的 linux 用户创建表出现异常

bash - 在 bash 中获取自定义日期值的 EPOCH 时间

linux - 显示.im6 : no decode delegate for this image format `/tmp/magick-KFcbfWUi'