shell 如何让curl/jq把一个包含空格的字符串当作一个字符串?

jyztefdp  于 2023-02-05  发布在  Shell
关注(0)|答案(1)|浏览(234)

我们如何让curl/jq把一个包含空格字符的字符串当作一个单独的字符串?
变量中包含JSON数据

authArray=[
  {
    "name": "testtoken1",
    "authType": "Token",
    "username": null,
    "password": "PASSWORD-MASKED",
    "header_1": "Authorization: Bearer {{@CmdCache | curl -s -d '{username: admin@qtech.ai, password: admin5665}' -H Content-Type:application/json -H Accept:application/json -X POST https://dev.qtech.ai/login | jq --raw-output .token}}",
    "header_2": null,
    "header_3": null,
    "clientId": null,
    "clientSecret": null,
    "id": null,
    "accessTokenUri": null,
    "authorizationScheme": null,
    "clientAuthenticationScheme": null,
    "tokenName": null,
    "scope": null,
    "grantType": null,
    "preEstablishedRedirectUri": null,
    "useCurrentUri": null,
    "userAuthorizationUri": null,
    "inactive": false,
    "invalid": false,
    "lastTestedOn": null,
    "passwordMasked": true
  },
  {
    "name": "Invalid_Auth",
    "authType": "Token",
    "username": null,
    "password": "PASSWORD-MASKED",
    "header_1": "Authorization:BearereyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
    "header_2": null,
    "header_3": null,
    "clientId": null,
    "clientSecret": null,
    "id": null,
    "accessTokenUri": null,
    "authorizationScheme": null,
    "clientAuthenticationScheme": null,
    "tokenName": null,
    "scope": null,
    "grantType": null,
    "preEstablishedRedirectUri": null,
    "useCurrentUri": null,
    "userAuthorizationUri": null,
    "inactive": false,
    "invalid": true,
    "lastTestedOn": null,
    "passwordMasked": true
  },
  {
    "name": "Invalid_Auth_Empty",
    "authType": "Token",
    "username": null,
    "password": "PASSWORD-MASKED",
    "header_1": "Authorization:Bearer",
    "header_2": null,
    "header_3": null,
    "clientId": null,
    "clientSecret": null,
    "id": null,
    "accessTokenUri": null,
    "authorizationScheme": null,
    "clientAuthenticationScheme": null,
    "tokenName": null,
    "scope": null,
    "grantType": null,
    "preEstablishedRedirectUri": null,
    "useCurrentUri": null,
    "userAuthorizationUri": null,
    "inactive": false,
    "invalid": true,
    "lastTestedOn": null,
    "passwordMasked": true
  },
  {
    "name": "Invalid_Auth_SQL",
    "authType": "Token",
    "username": null,
    "password": "PASSWORD-MASKED",
    "header_1": "Authorization:Bearer{{@Injection}}",
    "header_2": null,
    "header_3": null,
    "clientId": null,
    "clientSecret": null,
    "id": null,
    "accessTokenUri": null,
    "authorizationScheme": null,
    "clientAuthenticationScheme": null,
    "tokenName": null,
    "scope": null,
    "grantType": null,
    "preEstablishedRedirectUri": null,
    "useCurrentUri": null,
    "userAuthorizationUri": null,
    "inactive": false,
    "invalid": true,
    "lastTestedOn": null,
    "passwordMasked": true
  },
  {
    "name": "Default",
    "authType": "Basic",
    "username": "changeme@apisec.ai",
    "password": "PASSWORD-MASKED",
    "header_1": null,
    "header_2": null,
    "header_3": null,
    "clientId": null,
    "clientSecret": null,
    "id": null,
    "accessTokenUri": null,
    "authorizationScheme": null,
    "clientAuthenticationScheme": null,
    "tokenName": null,
    "scope": null,
    "grantType": null,
    "preEstablishedRedirectUri": null,
    "useCurrentUri": null,
    "userAuthorizationUri": null,
    "inactive": false,
    "invalid": false,
    "lastTestedOn": null,
    "passwordMasked": true
  }
]

我尝试使用下面动态更新的字符串和给定的代码更新该数组中名为testtoken 1的第一个对象的“header_1”字段

auth='"Authorization: Bearer {{@CmdCache | curl -s -d '{"username":"jackie@xxxx.com","password":"Welcome123"}' -H '"Content-Type: application/json"' -H '"Accept: application/json"' -X POST '"https://dev.qtech.ai/login"' | jq --raw-output '".token"' }}"'

mAuth=$(echo $authArray | jq 'map(select(.name == "testtoken1") |= (.header_1 = "'${auth}'" ))' | jq -c . )

当我执行上面的代码时,我得到下面的jq错误消息

jq: Unknown option -d
Use jq --help for help with command-line options,
or see the jq manpage, or online docs  at https://stedolan.github.io/jq

它抛出错误是因为auth变量中有空格字符,如果我从该变量中删除这些空格字符,它将正常工作。
以上命令输出最终需要与以下代码一起使用。

udto=$(echo $(_jq '.') | jq '.auths = '${mAuth}'')
curl -s --location --request PUT "${HOST}/api/v1/projects/$PROJECT_ID/env/$eId" --header "Accept: application/json" --header "Content-Type: application/json" --header "Authorization: Bearer "$token"" -d "$udto"

但我们的用例是这样的,我们希望按原样放置auth变量,这样我们就不必从应用程序的UI再次生成/添加空格字符,并且当我们通过按下“运行”按钮从UI运行该命令时,我们将在后台执行该curl结构/语法命令。
那么,我们如何使用curl、jq和bash shell脚本生成/传递该变量沿着空格字符呢?

vyu0f0g1

vyu0f0g11#

这是因为在展开变量时没有引用变量:

jq 'map(select(.name == "testtoken1") |= (.header_1 = "'${auth}'" ))'

应该是

jq 'map(select(.name == "testtoken1") |= (.header_1 = "'"${auth}"'" ))'

但是你首先不应该用shell变量插入jq程序/过滤器,而是定义一个jq变量,不用担心会弄乱你的jq过滤器:

jq --arg auth "$auth" 'map(select(.name == "testtoken1") |= (.header_1 = $auth))'

相关问题