如何将带有空格的参数传递给 docker run

Dav*_*vid 5 bash args docker

这可能特定于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}
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
 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
Run Code Online (Sandbox Code Playgroud)

Gor*_*son 2

在我看来,这是createUser函数中的一个错误,因为包含目录名称的各种变量缺少双引号。没有办法通过在参数中添加转义符、引号等来解决这个问题;您确实必须修复导致问题的脚本。

我还没有对此进行测试,但在第 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
Run Code Online (Sandbox Code Playgroud)

脚本中的其他地方可能会导致该问题,但至少需要进行上述更改。另外,我添加引号的第一行仅在某些版本的 bash 上需要它们,但最好将它们放在那里以防万一。

ps 第 39 行可能也应该修复:

IFS=':' read -a args <<< "$1"
Run Code Online (Sandbox Code Playgroud)

当前版本使用$@,这很奇怪。该函数只传递一个参数,如果不加引号,某些版本的 bash 会错误解析该参数,因此我拥有的版本是一种更好的方法。

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