高級開發(fā)運維測試必須掌握的envsubst命令使用詳解
0 | 是什么
envsubst
縮寫來自于 environment variable substitution
,即環(huán)境變量替換,是一個用于替換 shell 環(huán)境變量的工具。
它可以讀取輸入并在其中查找環(huán)境變量,然后將這些環(huán)境變量替換為其對應的值,最后輸出結(jié)果。在Linux和Unix操作系統(tǒng)上默認自帶。
通過這個命令,我們可以配置一些模板文件,然后通過定義環(huán)境變量的方式,將變量傳遞到模板文件中,從而動態(tài)地生成配置文件。在 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}
形式的環(huán)境變量的引用被替換為相應的值。如果給出了 SHELL-FORMAT,則僅替換那些在 SHELL-FORMAT 中引用的環(huán)境變量;否則,標準輸入中出現(xiàn)的所有環(huán)境變量引用都將被替換。
使用--variables
時,標準輸入被忽略,輸出由 SHELL-FORMAT 中引用的環(huán)境變量組成,每行一個。
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 中引用的環(huán)境變量組成
$ name='hello' ns='world' envsubst --variables '$name'< envsubst-template.yaml > envsubst-3.yaml $ cat envsubst-3.yaml name
- case 4 | 主要展示的是 --variables 這個 option 的作用:可以看到,當加上了這個變量后,標準輸入被忽略,輸出由 SHELL-FORMAT 中引用的環(huán)境變量組成, 每行一個
$ name='hello' ns='world' envsubst --variables '$name,$ns'< envsubst-template.yaml > envsubst-4.yaml $ cat envsubst-4.yaml name ns
以上就是高級開發(fā)運維測試必須掌握的envsubst命令使用詳解的詳細內(nèi)容,更多關于開發(fā)運維測試envsubst命令的資料請關注腳本之家其它相關文章!
相關文章
linux創(chuàng)建用戶useradd命令代碼示例
本文通過代碼示例給大家介紹了adduser與useradd命令二者的關系 以及使用useradd命令添加用戶的方法,需要的朋友參考下吧2017-08-08完美解決ntp的錯誤問題no server suitable for synchronization fo
下面小編就為大家?guī)硪黄昝澜鉀Qntp的錯誤問題no server suitable for synchronization fo。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03linux系統(tǒng)下用.sh文件執(zhí)行python命令的方法
這篇文章主要給大家介紹了關于linux系統(tǒng)下用.sh文件執(zhí)行python命令的相關資料,文中通過實例代碼介紹的非常詳細,對大家學習或者使用python具有一定的參考學習價值,需要的朋友可以參考下2022-07-07