如何将 JSON 中的值传递给 Docker 的 shell 脚本中的命令? [英] How to pass a value from JSON to a command in shell script at Docker?

查看:40
本文介绍了如何将 JSON 中的值传递给 Docker 的 shell 脚本中的命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 dockerfile 可以使用 Ubuntu 16.04 创建映像.在 docker 文件中:

# dotnet 工具命令运行 apt-get install dotnet-sdk-2.2 -y# 用于点网工具ENV PATH="${PATH}:/root/.dotnet/tools"# jq命令读取json文件运行 apt-get install jq -y添加 xxx/xxx# 复制deploy-tool.json文件添加 deploy-tool.json/xxx/deploy-tool.json# 复制主sh脚本到xxx添加 main-migrate.sh/xxx/main-migrate.sh# 运行主sh脚本,在每个xxx/*/bla..bla.sh中运行脚本运行 chmod +x/xxx/main-migrate.sh运行/permasense/main-migrate.sh

我的deploy-tool.json如下:

<代码>{"name": "xxx.DEPLOY",版本":1.2.3-dev.29"}

这里是main-migrate.sh

name = $(jq '.name'/xxx/deploy-tool.json)nugetFileVersion = $(jq '.version'/xxx/deploy-tool.json)# TODO 如何将值从 JSON 传递给下面的命令# 安装 dot net nugetdotnet 工具安装 -g $name --version "$nugetFileVersion" --add-source/xxx/

我在 xxx 文件夹中有 xxx.DEPLOY.nupkg.

dockerfile 运行 main-migrate.sh 时,我收到错误消息,抱怨 namenugetFileVersion找不到.

我如何传递name &nugetFileVersionjq 命令到dotnet tool install 上面的main-migrate.sh?>

谢谢

解决方案

这个想法是对的,但问题出在赋值语句中的 main-migrate.sh 脚本.Shell 赋值不会在 = 符号周围使用空格.应该是

name=$(jq '.name'/xxx/deploy-tool.json)# ^^^ 没有空格nugetFileVersion=$(jq '.version'/xxx/deploy-tool.json)# ^^^ 没有空格

I have a dockerfile to create an image with Ubuntu 16.04. Inside the docker file:

# dotnet tool command
RUN apt-get install dotnet-sdk-2.2 -y
# for dot net tool
ENV PATH="${PATH}:/root/.dotnet/tools"
# jq command to read json file
RUN apt-get install jq -y
ADD xxx /xxx
# Copy the deploy-tool.json file
ADD deploy-tool.json /xxx/deploy-tool.json
# Copy the main sh script to xxx
ADD main-migrate.sh /xxx/main-migrate.sh
# Run the main sh script to run script in each xxx/*/bla..bla.sh 
RUN chmod +x /xxx/main-migrate.sh
RUN /permasense/main-migrate.sh

my deploy-tool.json is as followed:

{
    "name": "xxx.DEPLOY",
    "version": "1.2.3-dev.29"
}

here is main-migrate.sh

name = $(jq '.name' /xxx/deploy-tool.json)
nugetFileVersion = $(jq '.version' /xxx/deploy-tool.json)

# TODO how to pass value from JSON to the command below
# install dot net nuget
dotnet tool install -g $name --version "$nugetFileVersion" --add-source /xxx/

I have the xxx.DEPLOY.nupkg in xxx folder.

When dockerfile runs main-migrate.sh, I got error message complaining that the name and nugetFileVersion cannot be found.

How do I pass name & nugetFileVersion from the jq command to the dotnet tool install above as shown in main-migrate.sh?

Thank you

解决方案

The idea is right, but the problem is with your main-migrate.sh script in the assignment statements. Shell assignments don't take spaces around the = symbol. It should have been

name=$(jq '.name' /xxx/deploy-tool.json)
#  ^^^ no spaces
nugetFileVersion=$(jq '.version' /xxx/deploy-tool.json)
#              ^^^ no spaces

这篇关于如何将 JSON 中的值传递给 Docker 的 shell 脚本中的命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆