zoukankan      html  css  js  c++  java
  • Shell 读取配置文件的方法

    配置文件config内容如下,文件名字为config

    ID=123
    IP=192.168.3.154
    Name=yangms

    想写个shell脚本,把这几个变量的值给读出来

    第一种方法: 用sed 流处理器,将每行=号和前面的部分去掉,并赋给变量。

    id=`sed '/^ID=/!d;s/.*=//' config`
    ip=`sed '/^IP=/!d;s/.*=//' config`
    name=`sed '/^Name=/!d;s/.*=//' config`
    echo $id
    echo $ip
    echo $name

    第二种方法: 使用eval方法解析。

    while read line;do
        eval "$line"
    done < config
    echo $ID
    echo $IP
    echo $Name

    第三种方法:直接将变量load进环境中成为环境变量。

    source config
    
    echo $ID
    echo $IP
    echo $Name

     参考博客:shell 读取配置文件的方法

                       linux shell 读取配置文件

                       shell 读取配置文件的方法

  • 相关阅读:
    Linux文件权限
    Linux命令
    LeetCode
    LeetCode
    LeetCode
    LeetCode
    LeetCode
    LeetCode
    LeetCode
    LeetCode
  • 原文地址:https://www.cnblogs.com/yangms/p/14461488.html
Copyright © 2011-2022 走看看