如何插入作为参数发送的字符串?
How to interpolate a string that was sent as an argument?
我对文件系统的访问权限有限,我想像这样设置通用通知处理程序调用:
notificator.sh "apples" "oranges" "There were $(1) and $(2) in the basket"
notificator.sh内容:
#!/bin/sh
echo
并得到如下所示的输出:
"There were apples and oranges in the basket"
这可能吗?如何实现?如果它是一个内置的 sh 解决方案,我会更喜欢。
我实际上试图通过 curl post 参数将结果字符串 ($3) 作为消息发送给电报机器人,但试图简化情况。
通过对您的 </code> 进行一些更改,我们可以轻松完成这项工作。</p>
<p>首先,让我们定义<code>
、</code>和<code>
:
$ set -- "apples" "oranges" 'There were ${one} and ${two} in the basket'
现在,让我们强制替换成 </code>:</p>
<pre><code>$ one= two= envsubst <<<""
There were apples and oranges in the basket
备注:
$(1)
尝试 运行 名为 1
的命令,并且可能会在您的脚本 运行 之前生成错误。请改用 ${var}
。
为了让这个方法起作用,我们需要重命名 </code> 中的变量。 </p></li>
<li><p><code>envsubst
是 GNU gettext-base
软件包的一部分,并且应该在 Linux 发行版中默认可用。
向 Charles Duffy 致敬。
脚本形式
考虑这个脚本:
$ cat script.sh
#!/bin/sh
echo "" | one= two= envsubst
我们可以执行上面的:
$ sh script.sh "apples" "oranges" 'There were ${one} and ${two} in the basket'
There were apples and oranges in the basket
作为替代方案(再次向 Charles Duffy 致敬),我们可以使用 here-doc:
$ cat script2.sh
#!/bin/sh
one= two= envsubst <<EOF
EOF
运行这个版本:
$ sh script2.sh "apples" "oranges" 'There were ${one} and ${two} in the basket'
There were apples and oranges in the basket
备选
以下脚本不需要envsubst
:
$ cat script3.sh
#!/bin/sh
echo "" | awk '{gsub(/$\{1\}/, a); gsub(/$\{2\}/, b)} 1' a= b=
运行 这个脚本加上我们的参数,我们发现:
$ sh script3.sh "apples" "oranges" 'There were and in the basket'
There were apples and oranges in the basket
$ sh script3.sh "apples" "oranges" 'There were and in the basket'
There were apples and oranges in the basket
我对文件系统的访问权限有限,我想像这样设置通用通知处理程序调用:
notificator.sh "apples" "oranges" "There were $(1) and $(2) in the basket"
notificator.sh内容:
#!/bin/sh
echo
并得到如下所示的输出:
"There were apples and oranges in the basket"
这可能吗?如何实现?如果它是一个内置的 sh 解决方案,我会更喜欢。 我实际上试图通过 curl post 参数将结果字符串 ($3) 作为消息发送给电报机器人,但试图简化情况。
通过对您的 </code> 进行一些更改,我们可以轻松完成这项工作。</p>
<p>首先,让我们定义<code>
、</code>和<code>
:
$ set -- "apples" "oranges" 'There were ${one} and ${two} in the basket'
现在,让我们强制替换成 </code>:</p>
<pre><code>$ one= two= envsubst <<<""
There were apples and oranges in the basket
备注:
$(1)
尝试 运行 名为1
的命令,并且可能会在您的脚本 运行 之前生成错误。请改用${var}
。为了让这个方法起作用,我们需要重命名
</code> 中的变量。 </p></li> <li><p><code>envsubst
是 GNUgettext-base
软件包的一部分,并且应该在 Linux 发行版中默认可用。
向 Charles Duffy 致敬。
脚本形式
考虑这个脚本:
$ cat script.sh
#!/bin/sh
echo "" | one= two= envsubst
我们可以执行上面的:
$ sh script.sh "apples" "oranges" 'There were ${one} and ${two} in the basket'
There were apples and oranges in the basket
作为替代方案(再次向 Charles Duffy 致敬),我们可以使用 here-doc:
$ cat script2.sh
#!/bin/sh
one= two= envsubst <<EOF
EOF
运行这个版本:
$ sh script2.sh "apples" "oranges" 'There were ${one} and ${two} in the basket'
There were apples and oranges in the basket
备选
以下脚本不需要envsubst
:
$ cat script3.sh
#!/bin/sh
echo "" | awk '{gsub(/$\{1\}/, a); gsub(/$\{2\}/, b)} 1' a= b=
运行 这个脚本加上我们的参数,我们发现:
$ sh script3.sh "apples" "oranges" 'There were and in the basket'
There were apples and oranges in the basket
$ sh script3.sh "apples" "oranges" 'There were and in the basket'
There were apples and oranges in the basket