Shell script-Read configuration file

Source: Internet
Author: User

Demand

Sometimes in the shell script needs some execution, if placed in the program is not easy to unified management, and every time you modify the path to the script inside to change the trouble, so the unified path in the configuration file easy to manage.

Problem

How to read the corresponding Key-value is the main problem, mainly using the IFS separator, remember to use the best back up the original IFS, use the end remember to restore IFS, otherwise there will be unknown error

configuration file (format: "key = value")

1 [CSV] 2 splithome = 20151214_ql/split_home3 output = 20151214_ql/output

Read Code (determine if the configuration file exists----Backup IFS-----read and output desired results-------restore IFS)

1 #!/bin/bash2 3 #Config file4config="Config.ini"5 6 #judge whether Config.ini exists or not.7 if[!-F"$config"];then8Echo"The file of Config.ini not exist"9 Exit 0Ten fi One  A #Save The old IFS -oldifs=$IFS -  the #set the IFS which is used to split the Key-value -ifs=" = " -  -split="Splithome" +output="Output" -  +s="" Ao="" at  -  whileRead-r Name Value -  Do - if[["$name"= * $split * ]]; Then -s=$value - elif[["$name"= * $output *]];then ino=$value - fi toDone <$config +  - #Output The result the Echo $s * Echo $o $ Panax Notoginseng #Reset the IFS -ifs=$OLDIFS the~

Command line output results

1 20151214_ql/split_home2 20151214_ql/output

Shell script-Read configuration file

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.