Step two: the replacement string
1) the replacement of only 1 substrings
格式:${var/old/new}
SCHOOL variable as in the foregoing test, to confirm the value of the variable:
[root@svr5 ~]# echo $SCHOOL
Tarena IT Group.
The first string 1 r replaced RRRR:
[root@svr5 ~]# echo ${SCHOOL/r/RRRR}
TaRRRRena IT Group.
2) replace all substring
format: $ {var // old / new }
In the foregoing SCHOOL variable as a test, to confirm the value of the variable:
[root@svr5 ~]# echo $SCHOOL
Tarena IT Group.
All r string are replaced RRRR:
[root@svr5 ~]# echo ${SCHOOL//r/RRRR}
TaRRRRena IT GRRRRoup.
3) Application Examples The reset value of the variable hostname
usage decomposition, will replace the current hostname domain suffix is "localdomain":
[root@svr5 ~]# echo $HOSTNAME //确认当前的主机名
svr5.tarena.com
[root@svr5 ~]# echo ${HOSTNAME/tarena.com/localdomain}
svr5.localdomain //替换后的字串
The current host name to replace the entire "localhost.localdomain":
[root@svr5 ~]# echo ${HOSTNAME/$HOSTNAME/localhost.localdomain}
localhost.localdomain
Suppose the new host name to be set is stored in the variable MYFQDN, the reset operation is as follows:
[root@svr5 ~]# MYFQDN="dbsvr.example.org" //新主机名变量
[root@svr5 ~]# hostname ${HOSTNAME/$HOSTNAME/$MYFQDN} //重设操作
[root@svr5 ~]# hostname //确认修改后的主机名
dbsvr.example.org
If you want to revert to the original host name, simply change the variable MYFQDN value, and then re-run again replace operation can be:
[root@svr5 ~]# MYFQDN="svr5.tarena.com" //定义要恢复的主机名
[root@svr5 ~]# hostname ${HOSTNAME/$HOSTNAME/$MYFQDN} //重设主机名
[root@svr5 ~]# hostname //确认恢复结果
svr5.tarena.com
After the introduction of variable to hold the host name, you can make Shell scripts have broader applicability.