【发布时间】:2019-10-18 00:09:07
【问题描述】:
我有一个关于如何提供包装在 '"..."' 中的变量的问题,这是 shell 脚本使用 read -d '' var <<- EOF 读取的值的预期格式。
概述
我希望使用这个jq 实用程序库:jq-hopkok。具体来说,bash shell 脚本to-components.sh,它使用jq 将URL 解析为其组件。
这个脚本很棒,除了我不清楚如何通过传递一个变量$URL来使用这个脚本。看来我只能使用静态 URL 字符串传递此脚本,该字符串必须包含在 double-quotes 中,然后再次包含在 single-quotes 中,例如'"..."'。
使用静态 URL 值 '"https://..."' 调用 to-components.sh 的示例
来自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 ...
谢谢
非常感谢任何建议!
【问题讨论】: