Bash:具有`read`命令和def的Shell脚本,包装在<

问题描述 投票:0回答:1

我对如何提供包装在'"..."'中的变量有疑问,该变量是shell脚本使用read -d '' var <<- EOF读取的值的预期格式。

概述

我希望使用此jq实用程序库:jq-hopkok。特别是bash shell脚本to-components.sh,它使用jq将URL解析为其组件。

该脚本很棒,除了,我不清楚如何通过将其传递给变量$URL来使用该脚本。看来我只能通过带有静态URL字符串的脚本传递此字符串,该字符串必须包装在双引号中,然后再次包装在单引号中,例如'"..."'

使用静态URL值to-components.sh调用'"https://..."'的示例

来自README.md

#!/usr/bin/env bash
# URL to components
echo '"https://server.example.com/deep/path/file.ext?with-a-parameter=true#and-a-fragment"' | ./to-components.sh

示例部分结果

{
  "value": "https://server.example.com/deep/path/file.ext?with-a-parameter=true#and-a-fragment",
  "valid": true,
  "scheme": {
    "value": "https",
    "valid": true
  },

读取to-components.sh中的URL字符串

shell脚本to-components.sh读取此带有特殊引号的URL字符串,必须将其包装为'"..."',否则它将无法解析为组件:

#!/usr/bin/env bash
set -e

# Split up a URL string to an object with the URL components.
read -d '' toComponents <<-'EOF' || true
...
EOF

cat | jq "$toComponents"

所需目标

我想用带有变量的URL字符串来调用此shell脚本,例如,我不确定如何构造变量???${URL}???,以便可以由shell脚本对其进行解析:

#!/usr/bin/env bash
URL="https://server.example.com/deep/path/file.ext?with-a-parameter=true#and-a-fragment"

# URL to components
echo ???${URL}??? | ./jq-hopkok/src/url/to-components.sh

我尝试了很多方法,但是没有用。所有导致的错误:parse error: Invalid numeric literal ...

谢谢

任何建议都非常感谢!

bash jq eof
1个回答
1
投票

您需要在$URL之前和之后回显文字双引号。清楚地写出来的一种方法是创建一个包含双引号的变量。]​​>

URL="https://server.example.com/deep/path/file.ext?with-a-parameter=true#and-a-fragment"
dq='"'
echo "$dq$URL$dq" | ./jq-hopkok/src/url/to-components.sh
© www.soinside.com 2019 - 2024. All rights reserved.