使用ssh时BASH无法运行查找

时间:2015-12-07 18:13:03

标签: bash ssh find

与我之前的问题相关 - bash test for existence of files on SFTP server

运行以下内容时:

Route::group(['domain' => '{subdomain}.tc.dev'], function()
{
    Route::any('/', function($subdomain)
    {
        return 'Subdomain ' . $subdomain;
    });
});

我明白了:

ssh remoteuser@1.2.3.4 find /home/remoteuser/logs -maxdepth 1 -type d

所以..看起来我无法通过ssh传递查找?

我真的需要找到一种检查文件的方法: /家庭/ remoteuser表/日志

供应商处理的日志将进入: /家庭/ remoteuser表/日志/存档

我确实需要查看文件是否已在日志中处理。

1 个答案:

答案 0 :(得分:1)

由于您的实际目标是确定任何文件是否直接存在于给定目录中,因此不需要find

考虑:

# this function is very careful to use only functionality built into POSIX shells.
rmtfunc() {
  set -- /home/remoteuser/logs/* # put contents of directory into $@ array
  for arg; do                    # ...for each item in that array...
    [ -f "$arg" ] && exit 0      # ...if it's a file that exists, success
  done
  exit 1                         # if nothing matched above, failure
}

# emit text that defines that function into the ssh command, then run same
if ssh remoteuser@host "$(declare -f rmtfunc); rmtfunc"; then
  echo "Found remote logfiles"
else
  echo "No remote logfiles exist"
fi

要诊断find无效的原因,请检查远程系统上的PATH:

ssh remoteuser@host 'echo "$PATH"' # important: single-quotes on the outside!

...并检查该PATH中的任何目录中是否确实存在find可执行文件。