我正在尝试编写一个Shell脚本,该脚本调用另一个脚本,然后执行rsync命令. 第二个脚本应在其自己的终端中运行,因此我使用了gnome-terminal -e "..."命令.该脚本的参数之一是一个字符串,其中包含应提供给rsync的参数.我把它们放在单引号中. 到这里为止,一切工作正常,直到rsync参数之一是包含空格的目录路径为止.我尝试了很多',,\",\'组合,但是脚本要么根本不运行,要么仅采用了路径的第一部分.
I'm trying to write a shell script that calls another script that then executes a rsync command. The second script should run in its own terminal, so I use a gnome-terminal -e "..." command. One of the parameters of this script is a string containing the parameters that should be given to rsync. I put those into single quotes. Up until here, everything worked fine until one of the rsync parameters was a directory path that contained a space. I tried numerous combinations of ',",\",\' but the script either doesn't run at all or only the first part of the path is taken.
这是我正在使用的代码的略微修改版本
Here's a slightly modified version of the code I'm using
gnome-terminal -t 'Rsync scheduled backup' -e "nice -10 /Scripts/BackupScript/Backup.sh 0 0 '/Scripts/BackupScript/Stamp' '/Scripts/BackupScript/test' '--dry-run -g -o -p -t -R -u --inplace --delete -r -l '\''/media/MyAndroid/Internal storage'\''' "在Backup.sh内运行此命令
rsync $5 "$path"其中目标$path是根据Stamp中的文本计算得出的.
where the destination $path is calculated from text in Stamp.
如何实现嵌套报价的这三个级别?
How can I achieve these three levels of nested quotations?
这些是我刚才看过的一个问题(我之前也尝试过其他来源)
These are some question I looked at just now (I've tried other sources earlier as well)
- unix.stackexchange/questions/23347/wrapping-a-command-that-includes-single-and-double-quotes-for-another-command
- 如何使嵌套的双引号在bash中生存口译员?
- 在bash中使用多层报价
- 嵌套引号bash
- unix.stackexchange/questions/23347/wrapping-a-command-that-includes-single-and-double-quotes-for-another-command
- how to make nested double quotes survive the bash interpreter?
- Using multiple layers of quotes in bash
- Nested quotes bash
我无法将解决方案应用于我的问题.
I was unsuccessful in applying the solutions to my problem.
推荐答案这里是一个示例. caller.sh使用gnome-terminal执行foo.sh,它依次打印所有参数,然后使用第一个参数调用rsync.
Here is an example. caller.sh uses gnome-terminal to execute foo.sh, which in turn prints all the arguments and then calls rsync with the first argument.
caller.sh :
#!/bin/bash gnome-terminal -t "TEST" -e "./foo.sh 'long path' arg2 arg3"foo.sh :
#!/bin/bash echo $# arguments for i; do # same as: for i in "$@"; do echo "$i" done rsync "$1" "some other path"如果$1包含多个要进行rsync的参数,其中一些是长路径,则上述方法将不起作用,因为bash会将"$1"作为一个参数传递,或者将作为多个参数,将其分割而无需考虑引号.
If $1 contains several parameters to rsync, some of which are long paths, the above won't work, since bash either passes "$1" as one parameter, or $1 as multiple parameters, splitting it without regard to contained quotes.
(至少)有一种解决方法,您可以按以下步骤欺骗bash:
There is (at least) one workaround, you can trick bash as follows:
caller2.sh :
#!/bin/bash gnome-terminal -t "TEST" -e "./foo.sh '--option1 --option2 \"long path\"' arg2 arg3"foo2.sh :
#!/bin/bash rsync_command="rsync $1" eval "$rsync_command"这等同于在命令行上键入rsync --option1 --option2 "long path".
This will do the equivalent of typing rsync --option1 --option2 "long path" on the command line.
警告:此黑客引入了一个安全漏洞,如果用户对字符串内容有任何影响,则可以精心制作$1执行多个命令(例如,'--option1 --option2 \"long path\"; echo YOU HAVE BEEN OWNED'将运行rsync然后执行 echo命令).
WARNING: This hack introduces a security vulnerability, $1 can be crafted to execute multiple commands if the user has any influence whatsoever over the string content (e.g. '--option1 --option2 \"long path\"; echo YOU HAVE BEEN OWNED' will run rsync and then execute the echo command).
更多推荐
Shell脚本中的三重嵌套引号
发布评论