高級(jí)開(kāi)發(fā)運(yùn)維測(cè)試必須掌握的envsubst命令使用詳解
0 | 是什么
envsubst
縮寫來(lái)自于 environment variable substitution
,即環(huán)境變量替換,是一個(gè)用于替換 shell 環(huán)境變量的工具。
它可以讀取輸入并在其中查找環(huán)境變量,然后將這些環(huán)境變量替換為其對(duì)應(yīng)的值,最后輸出結(jié)果。在Linux和Unix操作系統(tǒng)上默認(rèn)自帶。
通過(guò)這個(gè)命令,我們可以配置一些模板文件,然后通過(guò)定義環(huán)境變量的方式,將變量傳遞到模板文件中,從而動(dòng)態(tài)地生成配置文件。在 linux 運(yùn)維里,屬于比較高階但是很實(shí)用的一個(gè)命令。
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.
翻譯一下:
在正常操作模式下,標(biāo)準(zhǔn)輸入被復(fù)制到標(biāo)準(zhǔn)輸出,對(duì) $VARIABLE
或 ${VARIABLE}
形式的環(huán)境變量的引用被替換為相應(yīng)的值。如果給出了 SHELL-FORMAT,則僅替換那些在 SHELL-FORMAT 中引用的環(huán)境變量;否則,標(biāo)準(zhǔn)輸入中出現(xiàn)的所有環(huán)境變量引用都將被替換。
使用--variables
時(shí),標(biāo)準(zhǔn)輸入被忽略,輸出由 SHELL-FORMAT 中引用的環(huán)境變量組成,每行一個(gè)。
2 | 舉例說(shuō)明
首先,假設(shè)我有一個(gè)模板文件如下:
$ cat envsubst-template.yaml apiVersion: v1 kind: Deploy metadata: name: $name namespace: $ns
接著,分幾種 case 來(lái)使用 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 這個(gè) option 的作用:可以看到,當(dāng)加上了這個(gè)變量后,標(biāo)準(zhǔn)輸入被忽略,輸出由 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 這個(gè) option 的作用:可以看到,當(dāng)加上了這個(gè)變量后,標(biāo)準(zhǔn)輸入被忽略,輸出由 SHELL-FORMAT 中引用的環(huán)境變量組成, 每行一個(gè)
$ name='hello' ns='world' envsubst --variables '$name,$ns'< envsubst-template.yaml > envsubst-4.yaml $ cat envsubst-4.yaml name ns
以上就是高級(jí)開(kāi)發(fā)運(yùn)維測(cè)試必須掌握的envsubst命令使用詳解的詳細(xì)內(nèi)容,更多關(guān)于開(kāi)發(fā)運(yùn)維測(cè)試envsubst命令的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
shell中的循環(huán)語(yǔ)句、判斷語(yǔ)句實(shí)例
這篇文章主要介紹了shell中的循環(huán)語(yǔ)句、判斷語(yǔ)句實(shí)例,本文對(duì)shell的循環(huán)語(yǔ)句、判斷語(yǔ)句做了一個(gè)小結(jié),以及在使用中的注意事項(xiàng),需要的朋友可以參考下2014-07-07linux創(chuàng)建用戶useradd命令代碼示例
本文通過(guò)代碼示例給大家介紹了adduser與useradd命令二者的關(guān)系 以及使用useradd命令添加用戶的方法,需要的朋友參考下吧2017-08-08shell腳本編程之for語(yǔ)句、if語(yǔ)句使用介紹
這篇文章主要是介紹了shell腳本編程之for語(yǔ)句、if語(yǔ)句的使用方法,學(xué)習(xí)shell編程的朋友可以看下2013-09-09完美解決ntp的錯(cuò)誤問(wèn)題no server suitable for synchronization fo
下面小編就為大家?guī)?lái)一篇完美解決ntp的錯(cuò)誤問(wèn)題no server suitable for synchronization fo。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03linux系統(tǒng)下用.sh文件執(zhí)行python命令的方法
這篇文章主要給大家介紹了關(guān)于linux系統(tǒng)下用.sh文件執(zhí)行python命令的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-07-07