从 bash 脚本运行时出现 Rsync 语法错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22030280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rsync syntax error when run from bash script
提问by KroniK907
I have been working on a backup script that uses rsync to do an incremental backup.
我一直在研究使用 rsync 进行增量备份的备份脚本。
I have tested the following rsync command manually, and it runs and completes a backup without error:
我手动测试了以下 rsync 命令,它运行并完成备份没有错误:
rsync -aAXv --delete --progress --link-dest=/backup/Uyuk/Uyuk-backup-part1/2014-02-24/ /mnt/backup/ /backup/Uyuk/Uyuk-backup-part1/2014-02-25/
however when I run that same command in my backup script it gives me the following error:
但是,当我在备份脚本中运行相同的命令时,它给了我以下错误:
rsync: -aAXv --delete --progress --link-dest=/backup/Uyuk/Uyuk-backup-part1/2014-02-24/ /mnt/backup/ /backup/Uyuk/Uyuk-backup-part1/2014-02-25/: unknown option
rsync error: syntax or usage error (code 1) at main.c(1422) [client=3.0.6]
I ran bash -x on my script to figure out exactly what is sent to the console and here is what was printed:
我在我的脚本上运行 bash -x 以确定发送到控制台的确切内容,这是打印的内容:
+ rsync '-aAXv --delete --progress --link-dest=/backup/Uyuk/Uyuk-backup-part1/2014-02-24/ /mnt/backup/ /backup/Uyuk/Uyuk-backup-part1/2014-02-25/'
Does anyone see what is wrong? I cant find anything that would cause the syntax error.
有没有人看到有什么问题?我找不到任何会导致语法错误的东西。
EDIT: Here is the actual code I have in the script, and this is a pretty large script so yes some variables are not defined here, but you get the idea.
编辑:这是我在脚本中的实际代码,这是一个非常大的脚本,所以是的,这里没有定义一些变量,但你明白了。
mkdir -p "/backup/$HOST/$NAME/$TODAY"
#source directory
SRC="$MNT"
#link directory
LNK="/backup/$HOST/$NAME/$LAST/"
#target directory
TRG="/backup/$HOST/$NAME/$TODAY/"
#rsync options
OPT1="-aAXv --delete --progress --link-dest=$LNK"
#run the rsync command
echo "rsync $OPT1 $SRC $TRG"
rsync "$OPT1 $SRC $TRG" > /var/log/backup/backup.rsync.log 2>&1
回答by chepner
You are passing your option list as a single argument, when it needs to be passed as a listof arguments. In general, you should use an array in bash
to hold your arguments, in case any of them contain whitespace. Try the following:
你是通过你的选项列表作为一个参数,当需要作为传递它列表的参数。通常,您应该使用数组 inbash
来保存您的参数,以防它们中的任何一个包含空格。请尝试以下操作:
mkdir -p "/backup/$HOST/$NAME/$TODAY"
#source directory
SRC="$MNT"
#link directory
LNK="/backup/$HOST/$NAME/$LAST/"
#target directory
TRG="/backup/$HOST/$NAME/$TODAY/"
#rsync options
OPTS=( "-aAXv" "--delete" "--progress" "--link-dest=$LNK" )
#run the rsync command
echo "rsync $OPT1 $SRC $TRG"
rsync "${OPTS[@]}" "$SRC" "$TRG" > /var/log/backup/backup.rsync.log 2>&1
An array expansion ${OPTS[@]}
, when quoted, is treated specially as a sequence of arguments, each of which is quoted individually to preserve any whitespace or special characters in the individual elements. If arr=("a b" c d)
, then echo "${arr[@]}"
is the same as
${OPTS[@]}
引用时,数组扩展被特别视为一系列参数,每个参数都单独引用以保留单个元素中的任何空格或特殊字符。如果arr=("a b" c d)
,则echo "${arr[@]}"
与
echo "a b" "c" "d"
rather than
而不是
echo "a b c d"
This will not work in a shell that doesn't support arrays, but then, arrays were invented because there wasn't a safe way (that is, without using eval
) to handle this use case without them.
这在不支持数组的 shell 中不起作用,但是后来发明了数组,因为没有安全的方法(即,不使用eval
)来处理没有它们的用例。
回答by izilotti
The approach suggested by @chepner didn't work on my Mac OS X (10.9.4), but eval
did.
@chepner 建议的方法不适用于我的 Mac OS X (10.9.4),但适用eval
。
eval rsync "$OPT1 $SRC $TRG"
回答by twalberg
This:
这个:
rsync "$OPT1 $SRC $TRG"
passes all your intended arguments lumped together as one argument, which rsync
doesn't know how to deal with.
将所有您想要的参数作为一个参数一起传递,它rsync
不知道如何处理。
Try this instead:
试试这个:
rsync ${OPT1} ${SRC} ${TRG}