亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

高級(jí)開(kāi)發(fā)運(yùn)維測(cè)試必須掌握的envsubst命令使用詳解

 更新時(shí)間:2023年04月13日 10:14:08   作者:爛筆頭  
這篇文章主要為大家介紹了高級(jí)開(kāi)發(fā)運(yùn)維測(cè)試必須掌握的envsubst命令使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

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'&lt; envsubst-template.yaml &gt; 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)文章

最新評(píng)論