zoukankan      html  css  js  c++  java
  • Shell Script-读取配置文件


    需求


    有时候在shell script里面需要一些执行,如果放在程序里面不便于统一管理,并且每一次修改路径都要去script里面改好麻烦,所以统一把路径放在配置文件中方便管理。


    问题


    如何读取相对应的key-value是主要问题,主要是用到IFS分隔符,记住使用时最好先备份原来的IFS,使用完记得还原IFS,否则会出现未知错误


    配置文件(格式:”key = value“)

    1 [csv]
    2 splitHome = 20151214_QL/split_home
    3 output = 20151214_QL/output

    读取代码(判断配置文件是否存在----备份IFS-----读取并输出想要结果-------还原IFS)

     1 #!/bin/bash
     2 
     3 #config file
     4 config="config.ini"
     5 
     6 #judge whether config.ini exists or not.
     7 if [ ! -f "$config" ];then
     8         echo "the file of config.ini not exist"
     9         exit 0
    10 fi
    11 
    12 #save the old IFS
    13 OLDIFS=$IFS
    14 
    15 #set the IFS which is used to split the key-value
    16 IFS=" = "
    17 
    18 split="splitHome"
    19 output="output"
    20 
    21 s=""
    22 o=""
    23 
    24 while read -r name value
    25 do
    26 if [[ "$name" = *$split* ]]; then
    27         s=$value
    28 elif [[ "$name" = *$output* ]];then
    29         o=$value
    30 fi
    31 done < $config
    32 
    33 #output the result
    34 echo $s
    35 echo $o
    36 
    37 #reset the IFS
    38 IFS=$OLDIFS
    39 ~

    命令行输出结果

    1 20151214_QL/split_home
    2 20151214_QL/output
  • 相关阅读:
    团队展示
    原型设计(结对第一次)
    第二次作业——个人项目实战
    第一次作业--准备篇
    课程作业四
    课程作业三
    课程作业二
    课程作业一
    图像处理------ 一阶微分应用 (转载)
    dennis gabor 从傅里叶(Fourier)变换到伽柏(Gabor)变换再到小波(Wavelet)变换(转载)
  • 原文地址:https://www.cnblogs.com/loadofleaf/p/5055559.html
Copyright © 2011-2022 走看看