读取config.properties

本文介绍了一个用于从配置文件中读取特定键值的Shell脚本函数。该函数能够处理不同情况,如键不存在、键对应多个值等异常情况,并通过日志记录错误信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

function joey_read_value
{
if [ $# -ne 1 ]; then
joey_wrong_log "***Exit joey_read_value"
exit 1
fi
typeset joey_local_key=$1
if [ ! -f ${joey_config_file_name} ];then
joey_wrong_log "The config file not a file.config file is:${joey_config_file_name}"
echo "some thing wrong in easy_work.see the log:${joey_log_file}"
exit 1
fi
####
typeset joey_local_count=`cat ${joey_config_file_name}|awk -F= -v k="${joey_local_key}" '{ if ( $1 == k ) print $2 }'|wc -l`
if [ ${joey_local_count} -ge 2 ];then
joey_wrong_log "There are two same key in the config file.So the config wrong!"
joey_wrong_log "The key is :${joey_local_key}"
echo "some thing wrong in easy_work.see the log:${joey_log_file}"
exit 1
fi
if [ ${joey_local_count} -eq 1 ];then
typeset joey_local_value=`cat ${joey_config_file_name}|awk -F= -v k="${joey_local_key}" '{ if ( $1 == k ) print substr($0,(length($1)+2),length($0)) }'`
if [ $? -ne 0 ];then
joey_wrong_log "read the key in config Failed!Key:${joey_local_key}"
echo "some thing wrong in easy_work.see the log:${joey_log_file}"
exit 1
else
if [ "X${joey_local_value}" = "X" ];then
joey_wrong_log "read the key in config ,the key value is null!Key:${joey_local_key}"
echo "some thing wrong in easy_work.see the log:${joey_log_file}"
exit 1

else
joey_return=${joey_local_value}
return 0
fi
fi
else
joey_wrong_log "Can't find the value of the specified key \"${joey_local_key}\"!"
echo "some thing wrong in easy_work.see the log:${joey_log_file}"
exit 1
fi
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值