bash envsubst:在 Mac OS X 10.8 上找不到命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23620827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:50:37  来源:igfitidea点击:

envsubst: command not found on Mac OS X 10.8

bashshellterminalshcommand-line-interface

提问by Andrew

When I try to run a script that contains the envsubst command, I get this error. Looking online, this seems to be a standard bash command, so I am not sure what to install in order to get it to work.

当我尝试运行包含 envsubst 命令的脚本时,出现此错误。在网上看,这似乎是一个标准的 bash 命令,所以我不确定要安装什么才能让它工作。

回答by cobberboy

brew install gettext
brew link --force gettext 

This will enable envsubst on OS X, and force it to link properly. It requires homebrew to be installed.

这将在 OS X 上启用 envsubst,并强制它正确链接。它需要安装自制软件。

回答by ymonad

Edit: @cobberboy 's anwer is more correct. upvote him.

编辑:@cobberboy 的回答更正确。给他点赞。

brew install gettext
brew link --force gettext 

Following is my old answer:

以下是我的旧答案:

envsubstis included in gettextpackage.

envsubst包含在gettext包中。

Therefore you may compile it by your own, using standard build tools such as makeor using homebrew.

因此,您可以自己编译它,使用标准构建工具,例如make或 使用homebrew.

However, it seems to have little issue when installing gettextin MacOS. See following url for details: How to install gettext on MacOS X

但是,gettext在 MacOS 中安装时似乎没有什么问题。有关详细信息,请参阅以下网址:如何在 MacOS X 上安装 gettext

回答by mklement0

To clear up potential confusion:

清除潜在的混淆:

  • envsubstis an externalexecutable and thus not part of Bash; external executables are platform-dependent, both in terms of which ones are available as well as their specific behavior and the specific options they support (though, hopefully, there is a common subset based on the POSIX specifications)
  • Commands directly built into bashare called builtins, and only theycan be relied upon to be present on allplatforms.
  • envsubst是一个外部可执行文件,因此不是 Bash 的一部分;外部可执行文件是平台相关的,包括哪些可用以及它们的特定行为和它们支持的特定选项(尽管希望有一个基于 POSIX 规范的公共子集)
  • 直接内置的命令bash称为内建命令,只有它们才能出现在所有平台上

To test whether a given command is a builtin, use

测试给定命令是否是内置命令,请使用

type <cmdName>

In the case at hand, running type envsubston macOS 10.13 returns -bash: type: envsubst: not found, from which you can infer:

在手头的情况下,type envsubst在 macOS 10.13 上运行会返回-bash: type: envsubst: not found,您可以从中推断出:

  • envsubstis NOT a builtin
  • envsubstis not in your system's $PATH(and thus likely not present on your system)
  • envsubst不是内置的
  • envsubst不在您的系统中$PATH(因此可能不存在于您的系统中)

(By contrast, running the same on command on, e.g., a Ubuntu 12.04 system returns envsubst is hashed (/usr/bin/envsubst), which tells you that the utility is present and where it is located.)

(相比之下,在例如 Ubuntu 12.04 系统上运行相同的 on 命令会返回envsubst is hashed (/usr/bin/envsubst),它会告诉您该实用程序存在以及它所在的位置。)



A makeshift alternative to envsubstis to use eval, although the usual caveat applies: use evalonly on strings whose content you control or trust:

一个临时的替代方法envsubst是使用eval,尽管通常的警告适用:eval仅在您控制或信任其内容的字符串上使用:

Assume a sample.txtfile containing text with unexpanded variable references; e.g.:

假设一个sample.txt文件包含带有未扩展变量引用的文本;例如:

cat > sample.txt <<'EOF'
Honey, I'm $USER
and I'm $HOME.
EOF

The equivalent of:

相当于:

envsubst < sample.txt

is:

是:

eval "echo \"$(sed 's/"/\"/g' sample.txt)\""

There is a crucial difference, however:

然而,有一个关键的区别

  • envsubstexpands only environmentvariable references
  • whereas evalwill expand shellvariable references too- as well as embedded command substitutions, which is what makes use of evala security concern.
  • envsubst扩展环境变量引用
  • eval将扩大外壳变量引用-以及嵌入式命令替换,这是利用了eval一个安全问题。

回答by iRaS

I'm using this now in my bash script that requires envsubst:

我现在在需要 envsubst 的 bash 脚本中使用它:

if ! which envsubst > /dev/null 2>&1; then
    envsubst() {
        while read line; do
            line=$( echo $line | sed 's/"/\"/g' )
            eval echo $line
        done
    }
fi

you can use it as the envsubst command - of course it's not feature complete or something else:

您可以将它用作 envsubst 命令 - 当然它不是功能完整的或其他东西:

envsubst <<<'Honey, I am $HOME.'
envsubst < input > output 2> corrupt

回答by cyberz

If you don't want to bother installing homebrew and gettext, a one liner perl will do:

如果您不想费心安装自制软件和 gettext,可以使用单行 perl:

#!/usr/bin/perl -p
$_ =~ s/\Q${||}/$ENV{?:}/ while $_ =~ /($\{([^}]+)})|($(\w+))/g;