指定目录列表中的变量通过SSH bash脚本 [英] Assign directory listing to variable in bash script over ssh

查看:128
本文介绍了指定目录列表中的变量通过SSH bash脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想ssh到远程机器上,获得一个目录列表,将其分配给一个变​​量,然后我希望能够使用该变量在本地机器上的脚本的其余部分。

I'm trying to ssh into a remote machine, obtain a directory listing, assign it to a variable, and then I want to be able to use that variable in the rest of the script on the local machine.

在一些研究,并建立所有正确的按键和这样的,我可以运行通过ssh就好了命令。具体来说,如果我做的:

After some research and setting up all the right keys and such, I can run commands via ssh just fine. Specifically, if I do:

ssh -t user@server "ls /dir1/dir2/; exit; bash"

我得到一个目录列表。如果我不是做的:

I do get a directory listing. If I instead do:

ssh -t user@server "set var1=`ls /dir1/dir2/`; exit; bash"

而不是给出一个错误的LS该目录没有被发现。另外值得注意的是,在这之前,我问了SSH密钥密码,这让我觉得它在本地执行莫名其妙。

instead gives an ls error that the directory was not found. Also of note is that this happens before I am asked for the ssh key passphrase, which makes me think that it's executing locally somehow.

我如何可以创建一个局部变量目录列表在bash脚本的远程主机列表中的任何想法?

Any idea on how I can create a local variable with a directory listing list of the remote host in a bash script?

推荐答案

简单

var1=( $(ssh user@server ls /dir1/dir2) )

然后对其进行测试:

then test it:

for line in "${var1[@]}"; do echo "$line"; done

这是说,我想preFER

That said, I'd prefer

ssh user@server find /dir1/dir2 -maxdepth 1 -print0 | 
    xargs -0

这将


  • 处理特殊的文件名好多了

  • 更加灵活(人发现(1)

  • 添加型的F 来限制文件只

  • deal a lot better with special filenames
  • be more flexible (man find(1))
  • adding -type f to limit to files only

这篇关于指定目录列表中的变量通过SSH bash脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆