简体   繁体   English

Shell 脚本:读取 JSON 字符串输入参数

[英]Shell Script : Read JSON String Input Param

I have one shell script which accepts 3 arguments where 1st and 2nd are strings and 3rd is JSON enclosed within the double quote.我有一个 shell 脚本,它接受 3 个 arguments ,其中第一个和第二个是字符串,第三个是 JSON 括在双引号内。 Below is the snippet for shell script and args sample:下面是 shell 脚本和 args 示例的片段:

File readinp.sh文件 readinp.sh

param1=$1
param2=$2
param3=$3

echo "Param1 is $param1"

echo "Param2 is $param2"

echo "Param3 is $param3"

Script execution:脚本执行:

./readinp.sh hello world "[{'ParamName':'HostName','ParamVal':'Host1'},{'ParamName':'TargetMachine','ParamVal':'Machine1'}]"

How will I access the 3rd argument values as JSON inside my shell script?如何在我的 shell 脚本中访问第三个参数值作为 JSON? Please help me.请帮我。

Thanks in advance提前致谢

Shell is not the tool for this job. Shell 不是这项工作的工具。 Python, et. Python 等。 al., are better suited for the task. al.,更适合这项任务。 To run this you really need to trust that nobody is going to put malicious code in your input.要运行它,您确实需要相信没有人会将恶意代码放入您的输入中。 I would not run this code in production as I've written it because its security is sketchy and it is brittle, meaning you still need to know ahead of time what the vars are going to be named.我不会在生产中运行这段代码,因为我已经编写了它,因为它的安全性很粗略而且很脆弱,这意味着您仍然需要提前知道变量将被命名为什么。 At that point you might as well have pre-processed you json input in a separate script and passed just the values you want into your script.此时,您不妨在单独的脚本中对 json 输入进行预处理,并将您想要的值传递到脚本中。 Notice that jq expects valid json so your quotes have to reversed.请注意, jq需要有效的 json 因此您的报价必须颠倒。

#!/bin/sh
#
#
me="$( basename "${0}" )"

tmp="$( mktemp "${me}.XXXXXXXX" )" || exit 1
trap "rm -f -- '${tmp}'" EXIT
    
echo "$3" | jq -j '.[]|(.ParamName, "=\"", .ParamVal, "\"\n")' > "${tmp}"

. "${tmp}"

echo "\$1: $1"
echo "\$2: $2"
echo "\$3: $3"
echo "\$HostName: $HostName"
echo "\$TargetMachine: $TargetMachine"

Returns:回报:

$ ./readinp.sh hello world '[{"ParamName":"HostName","ParamVal":"Host1"},{"ParamName":"TargetMachine","ParamVal":"Machine1"}]'
$1: hello
$2: world
$3: [{"ParamName":"HostName","ParamVal":"Host1"},{"ParamName":"TargetMachine","ParamVal":"Machine1"}]
$HostName: Host1
$TargetMachine: Machine1

Slightly better with preprocessing but still brittle.预处理稍微好一点,但仍然很脆。 This I might use like in a trivial science project I'm working off the side of my desk, like a homework question where the code isn't going to be graded, but not in production:我可能会在我在办公桌边工作的一个琐碎的科学项目中使用它,比如一个家庭作业问题,其中代码不会被评分,但不会在生产中:

$ cat ./preinp.sh                    
#!/bin/sh

echo "'${1}'" "'${2}'"
echo "${3}" | jq '.[]|(.ParamVal)'

And:和:

$ cat ./readinp.sh                    
#!/bin/sh

p1="$1"
p2="$2"
HostName="$3"
TargetMachine="$4"

echo "\$p1: $p1"
echo "\$p2: $p2"
echo "\$HostName: $HostName"
echo "\$TargetMachine: $TargetMachine"

Results in:结果是:

$ ./preinp.sh 'single-word' 'with a space' '[{"ParamName":"HostName","ParamVal":"Host1"},{"ParamName":"TargetMachine","ParamVal":"Machine1"}]' | xargs ./readinp.sh
$p1: single-word
$p2: with a space
$HostName: Host1
$TargetMachine: Machine1

Final advice - don't do this.最后的建议 - 不要这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM