bash脚本中= <...的语法

问题描述

使用在stackoverflow.com上提出的问题的答案,我仅与远程主机上的最新文件进行同步

rsync -av --files-from=<(ssh remoteHost 'find pathToRemoteDir -mtime -2 -type f -exec basename {} \;') remoteHost:pathToRemoteDir .

从命令行(bash)执行时,它可以正常工作,但从bash脚本执行时,则不能。我收到以下错误

rsync: Failed to open files-from file <(ssh remoteHost 'find pathToRemoteDir -mtime -2 -type f -exec basename {} \;')

与bash专家相距甚远,我不明白为什么

解决方法

您想要的语法可能是:

'Asia/Kolkata'
  • rsync -av --files-from=- remoteHost:pathToRemoteDir . < <(ssh remoteHost 'find pathToRemoteDir -mtime -2 -type f -exec basename {} \;') :如果文件名是破折号,则表示--files-from=-

  • stdin:表示将commands1 < <(commands2)的输入重定向为commands1的输出。

基本上在这里用commands2告诉它取由rsync的输出生成的文件列表--files-from=-

此外,您不需要此特定于bash的重定向,它可以与标准管道一起使用:

< <(ssh remoteHost ...)

另一句话:

ssh remoteHost 'find pathToRemoteDir -mtime -2 -type f -exec basename {} \;' | rsync -av --files-from=- remoteHost:pathToRemoteDir . 是冒险且无用的。首选:

find pathToRemoteDir -mtime -2 -type f -exec basename {}

最后,如果文件名包含特殊字符或换行符,则以上语法将中断。

因此,最好将find pathToRemoteDir -mtime -2 -type f -printf '%f\n' --from0一起使用:

rsync