Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

第 15 章 envsubst - substitutes environment variables in shell format strings

目录

15.1. envsubst - substitutes environment variables in shell format strings
15.2. parallel - build and execute shell command lines from standard input in parallel
15.3. multitail
15.4. Logging
15.4.1. logger - a shell command interface to the syslog(3) system log module
15.5. Password
15.5.1. Shadow password suite configuration.
15.5.2. newusers - update and create new users in batch
15.5.3. chpasswd - update passwords in batch mode
15.5.4. sshpass - noninteractive ssh password provider

15.1. envsubst - substitutes environment variables in shell format strings

替代品在shell环境变量的格式字符串,类似模版替换操作

	
[root@localhost tmp]# echo "welcome $HOME ${USER:=a8m}" | envsubst
welcome /root root	
	
	
	
[root@localhost tmp]# cat config.template 
HOME=${HOME}
USER=${USER}

[root@localhost tmp]# envsubst < config.template > config.conf

[root@localhost tmp]# cat config.conf 
HOME=/root
USER=root
	
	

只替换 ${USER} 变量

	
[root@localhost tmp]# envsubst '${USER}' < config.template > config.conf
[root@localhost tmp]# cat config.conf 
HOME=${HOME}
USER=root	
	
	

模版变量

	
${var}				var值( 与 $var 相同)
${var-$DEFAULT}		如果未设置 var,则将表达式计算为 $DEFAULT
${var:-$DEFAULT}	如果未设置var或者为空,则将表达式计算为 $DEFAULT
${var=$DEFAULT}		如果未设置 var,则将表达式计算为 $DEFAULT
${var:=$DEFAULT}	如果未设置var或者为空,则将表达式计算为 $DEFAULT
${var+$OTHER}		如果为 var,则将表达式计算为 $OTHER,,否则为空字符串
${var:+$OTHER}		如果为 var,则将表达式计算为 $OTHER,,否则为空字符串