envsubst命令怎么使用

发布时间:2023-04-15 17:40:34 作者:iii
来源:亿速云 阅读:147

这篇文章主要介绍“envsubst命令怎么使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“envsubst命令怎么使用”文章能帮助大家解决问题。

0 | 是什么

envsubst 缩写来自于 environment variable substitution,即环境变量替换,是一个用于替换 shell 环境变量的工具。

它可以读取输入并在其中查找环境变量,然后将这些环境变量替换为其对应的值,最后输出结果。在Linux和Unix操作系统上默认自带。

通过这个命令,我们可以配置一些模板文件,然后通过定义环境变量的方式,将变量传递到模板文件中,从而动态地生成配置文件。在 linux 运维里,属于比较高阶但是很实用的一个命令。

1 | 怎么用

Usage: envsubst [OPTION] [SHELL-FORMAT]
Substitutes the values of environment variables.
Operation mode:
  -v, --variables             output the variables occurring in SHELL-FORMAT
Informative output:
  -h, --help                  display this help and exit
  -V, --version               output version information and exit
In normal operation mode, standard input is copied to standard output,
with references to environment variables of the form $VARIABLE or ${VARIABLE}
being replaced with the corresponding values.  If a SHELL-FORMAT is given,
only those environment variables that are referenced in SHELL-FORMAT are
substituted; otherwise all environment variables references occurring in
standard input are substituted.
When --variables is used, standard input is ignored, and the output consists
of the environment variables that are referenced in SHELL-FORMAT, one per line.

翻译一下:

在正常操作模式下,标准输入被复制到标准输出,对 $VARIABLE 或 ${VARIABLE} 形式的环境变量的引用被替换为相应的值。如果给出了 SHELL-FORMAT,则仅替换那些在 SHELL-FORMAT 中引用的环境变量;否则,标准输入中出现的所有环境变量引用都将被替换。

使用--variables时,标准输入被忽略,输出由 SHELL-FORMAT 中引用的环境变量组成,每行一个。

2 | 举例说明

首先,假设我有一个模板文件如下:

$ cat envsubst-template.yaml
apiVersion: v1
kind: Deploy
metadata:
  name: $name
  namespace: $ns

接着,分几种 case 来使用 envsubst,以便于更好的理解。

$ name='hello' ns='world' envsubst < envsubst-template.yaml > envsubst-1.yaml
$ cat envsubst-1.yaml
apiVersion: v1
kind: Deploy
metadata:
  name: hello
  namespace: world

注意, $ name='hello' ns='world' envsubst < envsubst-template.yaml > envsubst-1.yaml 也可以用export的方式定义:

$ export name='hello' 
$ export ns='world' 
$ envsubst < envsubst-template.yaml > envsubst-1.yaml
$ name='hello' ns='world' envsubst '$name'< envsubst-template.yaml > envsubst-2.yaml
$ cat envsubst-2.yaml 
apiVersion: v1
kind: Deploy
metadata:
  name: hello
  namespace: $ns
$ name='hello' ns='world' envsubst --variables '$name'&lt; envsubst-template.yaml &gt; envsubst-3.yaml
$ cat envsubst-3.yaml 
name
$ name='hello' ns='world' envsubst --variables '$name,$ns'< envsubst-template.yaml > envsubst-4.yaml
$ cat envsubst-4.yaml 
name
ns

关于“envsubst命令怎么使用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

推荐阅读:
  1. 示例详解Python3 or Python2 两者之间的差异
  2. 使用Django怎么限制用户访问频率

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

上一篇:Python面试最常问到的问题有哪些

下一篇:Java有序链表怎么合并

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》