【问题标题】:Copy the output of find command with scp使用 scp 复制 find 命令的输出
【发布时间】:2020-09-14 16:10:20
【问题描述】:

我正在尝试从本地机器,每天检查服务器中的新文件是什么,然后用一个 scp 复制它们。

我用ssh bastibast@192.168.0.200 'bash -s' < fileretriever.sh调用脚本

#!/usr/bin/env bash -x


FILES=$(find /home/bastibast/test -type f -mtime 0 )
echo "'$FILES'"
exit
scp -T -i ~/.ssh/id_rsa bastibast@192.168.0.200:"'$FILES'" ~/test/multiplecopy  

但是没有任何东西被复制到我的本地机器上。

echo "'$FILES'" 的输出: '/home/bastibast/test/test2 /home/bastibast/test/test1 /home/bastibast/test/script'

当我运行scp -T bastibast@192.168.0.200:'/home/bastibast/test/test2 /home/bastibast/test/test1 /home/bastibast/test/script' ~/test/multiplecopy

完美运行,将 3 个文件复制到我的本地计算机。为什么它在脚本中不起作用?

【问题讨论】:

  • 是故意使用exit 命令吗?在exit 之后脚本终止
  • 就是退出ssh连接。所以本地机器连接到远程服务器,找到所有新文件,存储在 $FILES 中。然后它断开连接以便能够从服务器 scp 所有上述文件 - 我不希望服务器 scp 到本地机器,但反过来。据我所知,如果不关闭前一个 ssh 会话,我就无法 scp。

标签: bash shell ssh scp


【解决方案1】:

只需使用 ssh 将命令 find 的输出提取到变量中,就可以更轻松地完成我想要做的事情

FILES=$(ssh bastibast@192.168.0.200 -- find /home/bastibast/test -type f -mtime 0 2>&1)

然后从那里开始 scp。

【讨论】:

    猜你喜欢
    • 2013-10-23
    • 2013-11-25
    • 2011-05-07
    • 1970-01-01
    • 2012-10-28
    • 1970-01-01
    • 1970-01-01
    • 2016-05-06
    • 2013-05-28
    相关资源
    最近更新 更多