脚本文件与命令行:rsync 和 --exclude
我有一个简单的测试 bash 脚本,如下所示:
#!/bin/bash
cmd="rsync -rv --exclude '*~' ./dir ./new"
$cmd # execute command
当我运行该脚本时,它也会复制以 ~
结尾的文件,即使我打算排除它们。 当我直接从命令行运行相同的 rsync 命令时,它起作用了! 有人知道为什么以及如何使 bash 脚本工作吗?
顺便说一句,我知道我也可以使用 --exclude-from
但我想知道它是如何工作的。
I have a simple test bash script which looks like that:
#!/bin/bash
cmd="rsync -rv --exclude '*~' ./dir ./new"
$cmd # execute command
When I run the script it will copy also the files ending with a ~
even though I meant to exclude them. When I run the very same rsync command directly from the command line, it works! Does someone know why and how to make bash script work?
Btw, I know that I can also work with --exclude-from
but I want to know how this works anyway.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试评估:
Try eval:
问题不在于您在脚本中运行它,而在于您将命令放入变量中,然后运行扩展的变量。 由于变量扩展发生在引号删除完成之后,因此排除模式周围的单引号永远不会被删除......因此 rsync 最终会排除名称以 ' 开头并以 ~' 结尾的文件。 要解决此问题,只需删除模式周围的引号(整个内容已经在双引号中,因此不需要它们):
...说到这里,为什么在运行之前将命令放入变量中? 一般来说,这是一个好方法,使代码比需要的更加混乱,并触发解析奇怪的情况(有些甚至比这更奇怪)。 那么怎么样:
The problem isn't that you're running it in a script, it's that you put the command in a variable and then run the expanded variable. And since variable expansion happens after quote removal has already been done, the single quotes around your exclude pattern never get removed... and so rsync winds up excluding files with names starting with ' and ending with ~'. To fix this, just remove the quotes around the pattern (the whole thing is already in double-quotes, so they aren't needed):
...speaking of which, why are you putting the command in a variable before running it? In general, this is a good way make code more confusing than it needs to be, and trigger parsing oddities (some even weirder than this). So how about:
您可以使用简单的
--einclude '~'
作为(根据手册页):You can use a simple
--eclude '~'
as (accoding to the man page):