Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

部分同步容器无法正确处理包含空格的 EXCLUDE/EXTRA #111

Open
1 of 4 tasks
taoky opened this issue Mar 31, 2024 · 3 comments
Open
1 of 4 tasks

部分同步容器无法正确处理包含空格的 EXCLUDE/EXTRA #111

taoky opened this issue Mar 31, 2024 · 3 comments

Comments

@taoky
Copy link
Member

taoky commented Mar 31, 2024

预期行为

当环境变量为:

EXTRA=--some-arg "Test String"

的时候,被 exec 的最终的程序能够知道 --some-arg 的参数是 "Test String"

实际行为

当写成:

exec some-program $EXTRA

的时候,shell 实际会(非预期地)拆成 --some-arg '"Test' 'String"',那么 --some-arg 的参数就是 "Test 了。

并且在有 * 的情况下,shell 可能会尝试进行文件名匹配(虽然几乎所有情况下都会失败)

复现步骤

  • Step 1
    EXTRA: --user-agent "Example Tool"
  • Step 2
    exec ... $EXTRA => error: unexpected argument 'Tool"' found

目前的同步程序中可能受影响(用 rg -F -- "--" 搜索)的有 (fixed or not):

  • rsync
  • rclone
  • tsumugu
  • yukina

lftpsync 的命令设计看起来不受此影响。

补充信息

https://superuser.com/a/1529316

@taoky
Copy link
Member Author

taoky commented Mar 31, 2024

参考 superuser stackexchange 的方法:

$ EXTRA="--user-agent 'Test client'"
$ extra_array=()
$ while IFS= read -r -d '' arg; do extra_array+=("$arg"); done < <(echo "$EXTRA" | xargs printf '%s\0')
$ echo "${extra_array[@]}"
--user-agent Test client
$ ./args "${extra_array[@]}"
2 args: <--user-agent> <Test client>

其中 ./args:

#!/bin/sh -
printf "%d args:" "$#"
printf " <%s>" "$@"
echo

@taoky
Copy link
Member Author

taoky commented Mar 31, 2024

xargs 的处理方法可能的不一致问题(这也是我们唯一有单引号的配置):

$ EXTRA="--exclude='.*' --exclude='.*/' --include='Raspberry_Pi_Education_Manual.pdf' --include=imager/ --include='raspios_.+/' --include=rpd_x86/"
$ extra_array=()
$ while IFS= read -r -d '' arg; do extra_array+=("$arg"); done < <(echo "$EXTRA" | xargs printf '%s\0')
$ ./args $EXTRA
6 args: <--exclude='.*'> <--exclude='.*/'> <--include='Raspberry_Pi_Education_Manual.pdf'> <--include=imager/> <--include='raspios_.+/'> <--include=rpd_x86/>
$ ./args "${extra_array[@]}"
6 args: <--exclude=.*> <--exclude=.*/> <--include=Raspberry_Pi_Education_Manual.pdf> <--include=imager/> <--include=raspios_.+/> <--include=rpd_x86/>

@knight42
Copy link
Member

knight42 commented Apr 3, 2024

bash 的分词是有点蛋疼.. 除了 superuser 里提到的这种方式外,我感觉可以改一下 entrypoint 让它把 "$@" 也传给同步程序,同时改一下 yukid 让它支持设置命令行参数,这样就可以避免需要手动对字符串进行分词的问题。

当初通过环境变量传参这种方式的意图是希望设置 flag 的值,而不是传一系列 flag。现在看这样也挺麻烦的,尤其是同步程序的 flag 比较多的时候😂。看你们觉得怎么处理比较合适了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants