Some tricks of the Linux shell

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.