如何在docker run命令中传递带有空格的参数

4
这可能与https://github.com/atmoz/sftp/blob/master/entrypoint#L36进行的参数解析有关。

但我正试图创建一个带有空格的目录:

我尝试了一些例子:


docker run -d  atmoz/sftp:alpine-3.7 user:password:::Inbound - Test Dir
...
[entrypoint] Parsing user data: "user:password:::Inbound"
Creating mailbox file: No such file or directory
[entrypoint] Creating directory: /home/user/Inbound
[entrypoint] Parsing user data: "-"
[entrypoint] ERROR: Invalid username "-", do not match required regex pattern: [A-Za-z0-9._][A-Za-z0-9._-]{0,31}

docker run -d atmoz/sftp:alpine-3.7 user:password:::"Inbound - Test Dir"
...
[entrypoint] Creating directory: /home/user/Inbound
[entrypoint] Creating directory: /home/user/-
[entrypoint] Creating directory: /home/user/Test
[entrypoint] Creating directory: /home/user/Dir

docker run -d atmoz/sftp:alpine-3.7 "user:password:::Inbound - Test Dir"
...
[entrypoint] Creating directory: /home/user/Inbound
[entrypoint] Creating directory: /home/user/-
[entrypoint] Creating directory: /home/user/Test
[entrypoint] Creating directory: /home/user/Dir

docker run -d atmoz/sftp:alpine-3.7 user:password:::Inbound\ -\ Test\ Dir
...
[entrypoint] Creating directory: /home/user/Inbound
[entrypoint] Creating directory: /home/user/-
[entrypoint] Creating directory: /home/user/Test
[entrypoint] Creating directory: /home/user/Dir

 docker run -d atmoz/sftp:alpine-3.7 user:password:::'Inbound - Test Dir'
...
[entrypoint] Creating directory: /home/user/Inbound
[entrypoint] Creating directory: /home/user/-
[entrypoint] Creating directory: /home/user/Test
[entrypoint] Creating directory: /home/user/Dir

1
参数只是由docker容器中的[entrypoint]解析。它在这里被读入${args[2]} here,然后在here周围分割。很奇怪,它们使用IFS=, read进行分割,整个过程似乎非常小心。尝试使用bash -x /entrypoint覆盖docker entrypoint以查看发生了什么。read没有-r标志,您可以使用它。请尝试双重转义'user:password:::Inbound\ -\ Test\ Dir' - KamilCuk
3
这段代码有漏洞,很简单。第101行的 for dirPath in ${dirArgs[@]} 无法处理包含空格的路径名。 - chepner
@KamilCuk 很遗憾,双重转义没有起作用。 - David
2个回答

2

我觉得这是createUser函数中的一个bug,由于缺少包含目录名称的各种变量的双引号。无法通过在参数中添加转义、引号等来解决此问题;您确实需要修复导致问题的脚本。

我没有测试过,但在第98-111行适当添加双引号可能会解决问题:

# Make sure dirs exists
if [ -n "$dir" ]; then
    IFS=',' read -a dirArgs <<< "$dir"    # Quotes added here
    for dirPath in "${dirArgs[@]}"; do    # And here
        dirPath="/home/$user/$dirPath"
        if [ ! -d "$dirPath" ]; then
            log "Creating directory: $dirPath"
            mkdir -p "$dirPath"               # And here
            chown -R $uid:users "$dirPath"    # And here
        else
            log "Directory already exists: $dirPath"
        fi
    done
fi

脚本中可能还有其他地方导致问题,但至少需要进行上述更改。此外,我添加的第一行只需要在某些版本的bash上加引号,但最好在那里加上引号以防万一。

另外,第39行也应该修复:

IFS=':' read -a args <<< "$1"

当前版本使用的是$@,这很奇怪。函数只会传递一个参数,如果没有引号包裹,一些bash版本会解析错误,所以我拥有的版本是更好的实现方式。

shellcheck.net指出了许多其他可疑的事情,但那是我认为应该重视的唯一问题。


谢谢,我已经向作者建议了这个补丁,并给予了您的功劳。https://github.com/atmoz/sftp/issues/143 - David
1
@DavidGoate 我建议你测试一下,确保它解决了问题(并且不会产生任何新问题 - 我相当确定不会有,但最好检查一下),并在你的错误报告中记录结果。 - Gordon Davisson
虽然 OP 的代码可能存在错误,但根本问题是如何在运行命令中包含空格。由于每个阅读此内容的人都在寻找答案以解决他们自己不同的代码中的这个问题,因此这种断言/回应通常并不有用。解决方法对社区来说没有好处,只对 OP 有用。 - Peter Kionga-Kamau

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接