SoFunction
Updated on 2025-03-09

Detailed explanation of the method of shell reading configuration files sed command

When writing a startup script, it involves reading the configuration file, and specifically records the way the shell script reads the startup file. It mainly provides two formats of reading methods. Method 1 is a configuration file that uses "[]" for partitioning, and Method 2 is a unique configuration item name in the configuration file.

The configuration file format is as follows:

# cat -n 
 #MYSQL configuration item [MYSQL]
 DB_HOST=192.168.0.1
 DB_PORT=3306
 DB_USER=root
 DB_PASSWD=mysql1234
 DB_NAME=system_manager1

 #MYSQL_1 configuration item [MYSQL_1]
 MYSQL_DB_HOST=192.168.0.2
 MYSQL_DB_PORT=2200
 MYSQL_DB_USER=root
 MYSQL_DB_PASSWD=mysql123456
 MYSQL_DB_NAME=bigdata1
 MYSQL_INIT_SQL='set slave;stop backup;'

 #REDIS configuration item [REDIS]
 DB_HOST=192.168.0.1
 DB_PORT=6379
 DB_ID=4
 DB_PASSWD=redis1234

Method 1: First try to use grep to get the start and end rows of the configuration item.

# grep -n -E '^\['  |grep -A 1 "\[MYSQL\]"|awk -F ':' '{print $1}'|xargs
2 10

# grep -n -E '^\['  |grep -A 1 "\[REDIS\]"|awk -F ':' '{print $1}'|xargs
19

Use sed to get the actual configuration based on the start and end rows

# sed -n "2,10 s/DB_HOST=//p" 
192.168.0.1

# sed -n "19,$ s/DB_PORT=//p" 
6379

Method 2: Use regular matching of unique configuration item names and display the actual configuration

# sed -r -n 's/(^MYSQL_INIT_SQL=)(.*)$/\2/p' 
'set slave;stop backup;'

# sed -r -n 's/(^MYSQL_DB_HOST=)(.*)$/\2/p' 
192.168.0.2

The complete script is as follows:

# cat 
#!/bin/bash
set -e

exit_script(){
   exit 1
}

if [ "$#" = 0 ]; then
    echo "Error parameter, command format: ./configfile"
    exit_script
else
    configPath=$1
fi

function get_line_num(){
    local configKey=$1
    grep -n -E '^\[' ${configPath} |grep -A 1 "\[${configKey}\]"|awk -F ':' '{print $1}'|xargs
}

function get_config(){
    #local configPath=$1
    local configKey=$1
    local configName=$2
    local line_num=$(get_line_num $configKey)
    local startLine=$(echo $line_num |awk '{print $1}')
    local endLine=$(echo $line_num|awk '{print $2}')
    if [ ${endLine} ];then
        sed -n "${startLine},${endLine} s/${configName}=//p" ${configPath}
    else
        sed -n "${startLine},$ s/${configName}=//p" ${configPath}
    fi
}

if [ -f $configPath ];then
    MYSQL_DB_HOST=$(get_config MYSQL DB_HOST)
else
    echo ${configPath}"The file does not exist, please check whether the configuration file exists"
    exit_script
fi
MYSQL_DB_PASSWD=$(get_config MYSQL DB_PASSWD)
MYSQL_DB_USER=$(get_config MYSQL DB_USER)
REDIS_DB_HOST=$(get_config REDIS DB_HOST)
REDIS_DB_PASSWD=$(get_config REDIS DB_PASSWD)

MYSQL_DB_HOST=$(sed -r -n 's/(^MYSQL_DB_HOST=)(.*)$/\2/p' $configPath)
MYSQL_DB_NAME=$(sed -r -n 's/(^MYSQL_DB_NAME=)(.*)$/\2/p' $configPath)
INIT_SQL=$(sed -r -n 's/(^MYSQL_INIT_SQL=)(.*)$/\2/p' $configPath)

echo "MYSQL_DB_HOST="${MYSQL_DB_HOST}
echo "MYSQL_DB_PASSWD="${MYSQL_DB_PASSWD}
echo "MYSQL_DB_USER="${MYSQL_DB_USER}
echo "REDIS_DB_HOST="${REDIS_DB_HOST}
echo "REDIS_DB_PASSWD="${REDIS_DB_PASSWD}

echo "------------------------------------------------------------------------------------------------------------------------------
echo "usesedRead configuration:MYSQL_DB_HOST="${MYSQL_DB_HOST}
echo "usesedRead configuration:MYSQL_DB_NAME="${MYSQL_DB_NAME}
echo "usesedRead configuration:MYSQL_INIT_SQL="${INIT_SQL}

Actual execution results:

# ./
The file does not exist, please check whether the configuration file exists

# ./
MYSQL_DB_HOST=192.168.0.2
MYSQL_DB_PASSWD=mysql1234
MYSQL_DB_USER=root
REDIS_DB_HOST=192.168.0.1
REDIS_DB_PASSWD=redis1234
--------------------------------------------------------------------------------------------------------------------------------
Use sed to read the configuration: MYSQL_DB_HOST=192.168.0.2
Use sed to read configuration: MYSQL_DB_NAME=bigdata1
Use sed to read configuration: MYSQL_INIT_SQL='set slave;stop backup;'

This is the end of this article about shell reading configuration file-sed command. For more relevant shell reading configuration file content, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!