高级开发运维测试必须掌握的envsubst命令使用详解

 更新时间:2023年04月13日 10:14:08   作者:烂笔头  
这篇文章主要为大家介绍了高级开发运维测试必须掌握的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,以便于更好的理解。

  • case 1 | envsubst-template 从 envsubst 里拿变量 name 和 ns 的值,然后将替换后的 yaml 重定向到 envsubst-1.yaml 里
$ 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
  • case 2 | 和 case 1 不同的地方是指定了 SHELL-FORMAT 为 $name, 意思是只将模板 yaml 里的 $name 变量用 envsubst 传入的变量替换掉,其他($ns)保持不变
$ 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
  • case 3 | 主要展示的是 --variables 这个 option 的作用:可以看到,当加上了这个变量后,标准输入被忽略,输出由 SHELL-FORMAT 中引用的环境变量组成
$ name='hello' ns='world' envsubst --variables '$name'&lt; envsubst-template.yaml &gt; envsubst-3.yaml
$ cat envsubst-3.yaml 
name
  • case 4 | 主要展示的是 --variables 这个 option 的作用:可以看到,当加上了这个变量后,标准输入被忽略,输出由 SHELL-FORMAT 中引用的环境变量组成, 每行一个
$ name='hello' ns='world' envsubst --variables '$name,$ns'< envsubst-template.yaml > envsubst-4.yaml
$ cat envsubst-4.yaml 
name
ns

以上就是高级开发运维测试必须掌握的envsubst命令使用详解的详细内容,更多关于开发运维测试envsubst命令的资料请关注脚本之家其它相关文章!

相关文章

  • Linux 使用grep筛选多个条件及grep常用过滤命令

    Linux 使用grep筛选多个条件及grep常用过滤命令

    这篇文章主要介绍了Linux 使用grep筛选多个条件及grep常用过滤命令,需要的朋友可以参考下
    2018-07-07
  • Linux expr 命令的使用小结

    Linux expr 命令的使用小结

    expr是命令行计算器,一般用在数字和字符串上面,本篇文章给大家介绍Linux expr 命令的使用小结,感兴趣的朋友跟随小编一起看看吧
    2023-10-10
  • 在Linux中实现scp命令自动输入密码的方法

    在Linux中实现scp命令自动输入密码的方法

    本文介绍了在Linux环境下使用scp命令自动输入密码的三种方法:SSH密钥认证、sshpass工具和expect脚本,推荐使用SSH密钥认证以确保数据传输的安全性,感兴趣的朋友一起看看吧
    2025-03-03
  • ansible执行shell脚本的方法

    ansible执行shell脚本的方法

    本文主要介绍了ansible执行shell脚本的方法,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • Linux查看端口占用情况的常用命令

    Linux查看端口占用情况的常用命令

    这篇文章主要给大家介绍了Linux查看端口占用情况的常用命令,lsof命令,netstat命令,ss命令,fuser命令,ps命令以及proc文件系统这几种常用命令,感兴趣的小伙伴跟着小编一起来看看吧
    2023-11-11
  • Linux 中批量提取文件夹下所有目录及文件名称

    Linux 中批量提取文件夹下所有目录及文件名称

    这篇文章主要介绍了Linux 中批量提取文件夹下所有目录及文件名称 ,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-05-05
  • Linux下is not in the sudoers file的解决方案

    Linux下is not in the sudoers file的解决

    当我们使用sudo命令切换用户的时候可能会遇到提示以下错误:用户名 is not in the sudoers file.本文给大家分享原因分析及解决方案,感兴趣的朋友跟随小编一起看看吧
    2023-02-02
  • 使用shell脚本分析网站日志统计PV、404、500等数据

    使用shell脚本分析网站日志统计PV、404、500等数据

    这篇文章主要介绍了使用shell脚本分析网站日志统计PV(浏览量)、404、500等数据,用一个脚本来实现,需要的朋友可以参考下
    2014-05-05
  • csh脚本语法实例

    csh脚本语法实例

    这篇文章主要介绍了csh脚本语法实例,小编看起来和bash shell也差不太多,需要的朋友可以参考下
    2014-11-11
  • Shell脚本实现自动安装zookeeper

    Shell脚本实现自动安装zookeeper

    这篇文章主要介绍了Shell脚本实现自动安装zookeeper,本文直接给出实现代码,需要的朋友可以参考下
    2015-01-01

最新评论