我对如何提供包装在'"..."'
中的变量有疑问,该变量是shell脚本使用read -d '' var <<- EOF
读取的值的预期格式。
我希望使用此jq
实用程序库:jq-hopkok。特别是bash shell脚本to-components.sh,它使用jq
将URL解析为其组件。
该脚本很棒,除了,我不清楚如何通过将其传递给变量$URL
来使用该脚本。看来我只能通过带有静态URL字符串的脚本传递此字符串,该字符串必须包装在双引号中,然后再次包装在单引号中,例如'"..."'
。
'"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
},
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 ...
任何建议都非常感谢!
您需要在$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