I normally use scp to copy stuff, but now I'm trying to get used to the more powerful rsync command. It helps me use less bandwidth by copying up only files that have changed. However, rsync has a lot of complex parameters, so I thought, hey, I'll just make a little Bash script that makes it easy for me, and call the command 'rscp'. So, off I went building something like this. Note in the example below that my web host uses a different port number besides 22, so that's why the
I normally use scp to copy stuff, but now I'm trying to get used to the more powerful rsync command. It helps me use less bandwidth by copying up only files that have changed. However, rsync has a lot of complex parameters, so I thought, hey, I'll just make a little Bash script that makes it easy for me, and call the command 'rscp'. So, off I went building something like this. Note in the example below that my web host uses a different port number besides 22, so that's why the $1 is used for that.
我通常使用scp来复制内容,但现在我正在尝试使用更强大的rsync命令。它通过仅复制已更改的文件来帮助我减少带宽使用。但是,rsync有很多复杂的参数,所以我想,嘿,我只是制作一个让我很容易的Bash脚本,并调用命令'rscp'。所以,我开始建造这样的东西。请注意,在下面的示例中,我的Web主机使用了除22之外的其他端口号,因此这就是使用$ 1的原因。
#!/bin/bash
rsync -avzp --progress --rsh='ssh -p$1' $2 $3 $4 $5 $6 $7
So, its usage, I hoped, would be something like:
所以,我希望它的用法如下:
rscp 3822 --exclude=tiny_mce /var/www/mysite/* [email protected]:~/www/mysite
That would make it a little bit closer to my usage of the scp command, you see.
你知道,这会让我更接近我对scp命令的使用。
However, when I ran this, I get this error:
但是,当我运行它时,我收到此错误:
building file list ...
4 files to consider
ERROR: destination must be a directory when copying more than 1 file
rsync error: errors selecting input/output files, dirs (code 3) at main.c(494) [receiver=2.6.9]
rsync: connection unexpectedly closed (8 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at io.c(454) [sender=2.6.9]
But if I go back to the regular rsync command, it works just fine.
但是如果我回到常规的rsync命令,它就可以了。
Can anyone figure out what I'm doing wrong in my Bash script?
任何人都可以在我的Bash脚本中弄清楚我做错了什么吗?
2 个解决方案
#1
7
/var/www/mysite/* is expanded by the shell, so you have many more arguments being passed in than you're handling in the script.
/ var / www / mysite / *由shell扩展,因此传入的参数比在脚本中处理的参数多得多。
Also variables aren't substituted in single quotes, so you need to use double quotes. (See bash man page)
此外,变量不能用单引号替换,因此您需要使用双引号。 (参见bash手册页)
Try this:
#!/bin/bash
PORT=$1
shift
rsync -avzp --progress --rsh="ssh -p$PORT" "$@"
First we take the port off the argument list (with shift), then we use "$@" which is special, it refers to the argument (to the script in this case), and is converted to all of them, but each separately in double quotes. (see the bash man page for more details).
首先我们将端口从参数列表中取出(使用shift),然后我们使用“$ @”这是特殊的,它引用参数(在本例中为脚本),并转换为所有这些,但每个单独用双引号。 (有关详细信息,请参阅bash手册页)。
#2
0
Couldn't you technically turn this into a function as well?:
难道你不能在技术上把它变成一个函数吗?:
rscp() {
rsync -avzp --progress --rsh="ssh -p$PORT" "$@"
}
And then when you run it: rscp 3822
然后当你运行它:rscp 3822
is used for that.I normally use scp to copy stuff, but now I'm t