This question already has an answer here:
这个问题在这里已有答案:
- Bash: need help passing a a variable to rsync 3 answers
Bash:需要帮助将变量传递给rsync 3答案
I have the following for-loop which loops through all the given sources that need to be copied.
我有以下for循环,循环遍历所有需要复制的给定源。
for i in "${sources[@]}"; do
exclude="--exclude 'exclude_folder/exclude_file'"
rsync -az $exclude $i $destination
done
However, the exclude option is not working.
但是,排除选项不起作用。
for i in "${sources[@]}"; do
exclude="--exclude 'exclude_folder/exclude_file'"
rsync -az "$exclude" "$i" "$destination"
done
If I use the code above, rsync will exit and give an error that it is an unknown option.
如果我使用上面的代码,rsync将退出并给出一个错误,它是一个未知的选项。
If I simply use the following code, it works, but I want to use a variable for the exclude option.
如果我只使用以下代码,它可以工作,但我想使用一个变量作为exclude选项。
for i in "${sources[@]}"; do
rsync -az --exclude 'exclude_folder/exclude_file' $i $destination
done
2 个解决方案
#1
-1
I would use eval
.
我会用eval。
Your code:
for i in "${sources[@]}"; do
exclude="--exclude 'exclude_folder/exclude_file'"
rsync -az "$exclude" "$i" "$destination"
done
would be then (I'm trying to be as close to your logic as possible):
那么(我想尽可能接近你的逻辑):
for i in "${sources[@]}"; do
exclude="--exclude 'exclude_folder/exclude_file'"
rsync_command="rsync -az $exclude $i $destination"
eval rsync_command
done
From the eval
man pages:
从评估手册页:
eval
Evaluate several commands/arguments
评估几个命令/参数
Syntax eval [arguments]
语法eval [arguments]
The arguments are concatenated together into a single command, which is then read and executed, and its exit status returned as the exit status of eval. If there are no arguments or only empty arguments, the return status is zero.
参数连接在一起形成一个命令,然后读取并执行该命令,并将其退出状态作为eval的退出状态返回。如果没有参数或只有空参数,则返回状态为零。
eval is a POSIX `special' builtin
eval是一个POSIX“特殊”内置
EDIT
Gordon Davisson is right about the bugs/insecurities in eval
. If any other solution is available then it is better to use it. Here the bash arrays are better. The array answer is superior answer.
戈登戴维森对于评估中的错误/不安全感是正确的。如果有任何其他解决方案可用,那么最好使用它。这里的bash数组更好。阵列答案是出色的答案。
Please see the answer at Bash: need help passing a a variable to rsync
请参阅Bash的答案:需要帮助将变量传递给rsync
#2
0
Example list directories to exclude (also wildcharts) :
要排除的示例列表目录(也是wildcharts):
#!/bin/sh
export PATH=/usr/local/bin:/usr/bin:/bin
LIST="rootfs usr data data2"
for d in $LIST; do
rsync -az --exclude /$d/ .....
done
#1
-1
I would use eval
.
我会用eval。
Your code:
for i in "${sources[@]}"; do
exclude="--exclude 'exclude_folder/exclude_file'"
rsync -az "$exclude" "$i" "$destination"
done
would be then (I'm trying to be as close to your logic as possible):
那么(我想尽可能接近你的逻辑):
for i in "${sources[@]}"; do
exclude="--exclude 'exclude_folder/exclude_file'"
rsync_command="rsync -az $exclude $i $destination"
eval rsync_command
done
From the eval
man pages:
从评估手册页:
eval
Evaluate several commands/arguments
评估几个命令/参数
Syntax eval [arguments]
语法eval [arguments]
The arguments are concatenated together into a single command, which is then read and executed, and its exit status returned as the exit status of eval. If there are no arguments or only empty arguments, the return status is zero.
参数连接在一起形成一个命令,然后读取并执行该命令,并将其退出状态作为eval的退出状态返回。如果没有参数或只有空参数,则返回状态为零。
eval is a POSIX `special' builtin
eval是一个POSIX“特殊”内置
EDIT
Gordon Davisson is right about the bugs/insecurities in eval
. If any other solution is available then it is better to use it. Here the bash arrays are better. The array answer is superior answer.
戈登戴维森对于评估中的错误/不安全感是正确的。如果有任何其他解决方案可用,那么最好使用它。这里的bash数组更好。阵列答案是出色的答案。
Please see the answer at Bash: need help passing a a variable to rsync
请参阅Bash的答案:需要帮助将变量传递给rsync
#2
0
Example list directories to exclude (also wildcharts) :
要排除的示例列表目录(也是wildcharts):
#!/bin/sh
export PATH=/usr/local/bin:/usr/bin:/bin
LIST="rootfs usr data data2"
for d in $LIST; do
rsync -az --exclude /$d/ .....
done