用于执行命令的外壳程序脚本,该命令接受命令行参数并将输出存储到变量

时间:2018-07-05 13:56:43

标签: shell jq

当我执行以下命令时,它正在打印必需的内容,即abcd1g2f

cat priv.json | jq '.data | map(select(.properties | any(.[]; .value == "client_release")) |.id)[0]'

但是当我从shell脚本执行相同命令(这是必需的)时,它会打印null。有人可以帮我吗?

下面是我写的shell脚本(test.sh):

get_priv0=$(cat priv.json | jq '.data | map(select(.properties | any(.[]; .value == "$1")) |.id)[0]')
echo $get_priv0
我执行的

命令

./test.sh client_release

JSON文件(priv.json)如下:

{
"data": [{
    "id": "abcd1g2f",
    "resourceURI": "https://somerepo.com/service/local/privileges/abcd1g2f",
    "name": "release1",
    "description": "release1",
    "type": "target",
    "userManaged": true,
    "properties": [{
        "key": "repositoryGroupId",
        "value": ""
    }, {
        "key": "method",
        "value": "create,read"
    }, {
        "key": "repositoryId",
        "value": "client_release"
    }, {
        "key": "repositoryTargetId",
        "value": "1"
    }]
}, {
    "id": "asdf1k4g",
    "resourceURI": "https://somerepo.com/service/local/privileges/asdf1k4g",
    "name": "release2",
    "description": "release2",
    "type": "target",
    "userManaged": true,
    "properties": [{
        "key": "repositoryGroupId",
        "value": ""
    }, {
        "key": "method",
        "value": "read"
    }, {
        "key": "repositoryId",
        "value": "formal_release"
    }, {
        "key": "repositoryTargetId",
        "value": "1"
    }]
}, {
    "id": "hirk5d7b3l",
    "resourceURI": "https://somerepo.com/service/local/privileges/hirk5d7b3l",
    "name": "release3",
    "description": "release3",
    "type": "target",
    "userManaged": true,
    "properties": [{
        "key": "repositoryGroupId",
        "value": ""
    }, {
        "key": "method",
        "value": "create,read"
    }, {
        "key": "repositoryId",
        "value": "client_release"
    }, {
        "key": "repositoryTargetId",
        "value": "1"
    }]
}]
}

0 个答案:

没有答案