1. 程式人生 > >解決shell讀取配置檔案key帶點號(.)的問題

解決shell讀取配置檔案key帶點號(.)的問題

傳統source讀取方式

#application.properties

db.uat.user=user
db.uat.password=password
db.uat.url=https://www.baidu.com


#!/bin/sh

source "application.properties"

echo $db.uat.user
echo $db.uat.password

curl $db.uat.url

執行start.sh出現command not found.

解決方法:

改成如下方式讀取

#!/bin/sh

file="application.properties"

if [ -f "$file" ]
then
    echo "$file found."
    while IFS='=' read -r key value
    do
        key=$(echo $key | tr .-/ _ | tr -cd 'A-Za-z0-9_')
        if [ "$value"x != x ]; then      	    
		    eval ${key}=\${value}
        fi
    done < "$file"
else
	echo "$file not found."
fi

echo $db_uat_user
echo $db_uat_password

curl $db_uat_url

效果如下: