The first example:
A document content:
[[Email protected] -~]#Cat/data/public_jdbh/SH/conf/Test.json {"value1": ServerID,"Value1":"NAME", "Value1": Idnum,"Value1":" Time","Gamedburl":"Mysql_user; MYSQL_PASSWD; user_db; Mysql_host; Mysql_port", "Logdburl":"Mysql_user; mysql_passwd;other_db; Mysql_host; Mysql_port",}
Shell script:
#!/bin/bash
Dirname=/tmp
Configfile=test.json
serverid=222
Name= "Test"
idnum=333
Time= "2017-12-11 10:10:10"
Mysql_user= "Root"
Mysql_passwd= "123456"
mysql_port=3306
User_db=user
Other_db=other
forOPTIONinch{"ServerID","NAME","Idnum"," Time","Mysql_user","mysql_passwd","user_db","Mysql_host","Mysql_port"," Other_db"} Do sed-r-i'[email protected]' "${option}"'@' "${! OPTION}"'@g'${dirname}/${configfile} Done
Perform:
[[Email protected] -tmp]#SHTest.SH[[Email protected]- -tmp]#CatTest.json {"value1":222, "value1":"Test", "value1":333, "value1":"2017-12-11 10:10:10", "Gamedburl":"Root;123456;user;; 3306", "Logdburl":"Root;123456;other;; 3306",} [[Email protected]- -tmp]#
Explanation:
forOPTIONinch{"ServerID","NAME","Idnum"," Time","Mysql_user","mysql_passwd","user_db","Mysql_host","Mysql_port","other_db"} Do sed-r-i'[email protected]' "${option}"'@' "${! OPTION}"'@g'${dirname}/${configfile}# #OPTION变量的内容就是 {"ServerID", "NAME", "Idnum", "Time", "Mysql_user", "mysql_passwd", "user_db", "Mysql_ HOST "," Mysql_port "," other_db "}. These strings are to be consistent with the document Test.json.
# #然后使用sed匹配到option在文档的内容,
# #然后把 ${! The value of option} is replaced with the content of option.
##${! The value of the option} variable is the contents of the variable of option's value
Example 2:
[[Email protected] thetmp]# a= (1 2 3 4) [[email protected]- thetmp]#Echo${a[2]}3[[Email protected]- thetmp]#Echo${a[@]}1 2 3 4[[Email protected]- thetmp]#Echo${!a[@]}0 1 2 3[[Email protected]- thetmp]#Echo${a[@]}1 2 3 4[[Email protected]- thetmp]#Echo${!a[@]}0 1 2 3
# #使用 ${!a[@]} to get the subscript of the array
Some tricks of the Linux shell