Linux Shell 学习笔记三

在Shell中调用Golang代码

go run .go

Jq

jq在线验证:https://jqplay.org/jq

jq-to_entries-map-from_entries

[jiankunking@hddxps8156 jiankunking]# cat jq-entries.sh 
#!/bin/bash
data='{"a": "xxxx","b": "yyyy"}'
a=20
echo `echo $data| 
  jq 'to_entries'`

data=`echo $data| 
  jq 'to_entries | 
    map(if .key == "a" 
        then . + {"value":'$a'} 
        else . 
        end
        ) | 
    from_entries'`

echo $data    
if [ `echo $data | jq '.a'` -eq 20 ];then \
    echo 666
fi

data=`echo $data | sed 's/ //g'`
echo $data
[jiankunking@hddxps8156 jiankunking]# ./jq-entries.sh
[ { "key": "a", "value": "xxxx" }, { "key": "b", "value": "yyyy" } ]
{ "a": 20, "b": "yyyy" }
666
{"a":20,"b":"yyyy"}

jq-to_entries-select

[jiankunking@hddxps8156 jiankunking]# kubectl get deployment test-admin -n i-test -ojson
{
    "apiVersion": "apps/v1",
    "kind": "Deployment",
    "metadata": {
        "labels": {
            "app": "test-admin",
            "app.kubernetes.io/name": "test-admin",
            "app.kubernetes.io/version": "v0"
        },
        "name": "test-admin",
        "namespace": "i-test",
        "resourceVersion": "2257518831",
        "uid": "4519e6a3-a881-4b16-9cff-83d2d7425bf6"
    },
    "spec": {},
    "status": {}
}
[jiankunking@hddxps8156 jiankunking]# kubectl get deployment test-admin -n i-test -ojson | jq '.metadata.labels'
{
  "app": "test-admin",
  "app.kubernetes.io/name": "test-admin",
  "app.kubernetes.io/version": "v0"
}
[jiankunking@hddxps8156 jiankunking]# kubectl get deployment test-admin -n i-test -ojson | jq '.metadata.labels'| jq -r 'to_entries[]| select(.key=="app.kubernetes.io/name")| .value'
test-admin
[jiankunking@hddxps8156 jiankunking]# 

作者:jiankunking 出处:http://blog.csdn.net/jiankunking

你可能感兴趣的:(Linux,Shell,linux,学习,笔记,shell)