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

标签 bash docker args

这可能特定于 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

最佳答案

在我看来,这是 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

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

附注第 39 行可能也应该被修复:

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

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

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

关于bash - 如何将参数传递给带空格的 docker run,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53072378/

相关文章:

linux - 提到的语法错误是在本例中使用 Bash 的项目

ruby 使用变量执行 bash 命令

linux - 为什么重定向符号会改变 ls 的行为?

docker - Gitlab CI。来自守护进程 : No such image 的错误响应

Docker 连接到主机 mysql 时出错

linux - 将 cron 日志存储在 timedate 文件中

docker - config.v2.json中的docker healthcheck

python - 如何从数据库动态获取表?

没有参数的 c optarg atoi

java 101,我如何计算传递给 main 的参数数量?