您现在的位置是:网站首页> 编程资料编程资料
shell脚本如何读取properties文件中的值_linux shell_
2023-05-26
381人已围观
简介 shell脚本如何读取properties文件中的值_linux shell_
如下面代码所示的properties文件是各种编程语言中常用的属性文件,通过key读取value是极其常见的需求。
# 端口 server.port=8520 # 上传文件总的最大值 spring.servlet.multipart.max-request-size=10MB # 单个文件的最大值 spring.servlet.multipart.max-file-size=10MB
Linux中的shell通常是需要程序员自己写一个方法实现对properties文件的读取。以下是我写的一个方法,亲测有效,欢迎各位取用。
#读取属性文件指定键的值 get_value_of_properties_file() { result="" proFilePath="$1" key="$2" if [ "WJA${key}" = "WJA" ]; then echo "参数错误,未能指定有效Key。" echo "" >&2 exit 1 fi if [ ! -f ${proFilePath} ]; then echo "属性文件(${proFilePath})不存在。" echo "" >&2 exit 1 fi if [ ! -r ${proFilePath} ]; then echo "当前用户不具有对属性文件(${proFilePath})的可读权限。" echo "" >&2 exit 1 fi keyLength=$(echo ${key}|wc -L) lineNumStr=$(cat ${proFilePath} | wc -l) lineNum=$((${lineNumStr})) for ((i = 1; i <= ${lineNum}; i++)); do oneLine=$(sed -n ${i}p ${proFilePath}) if [ "${oneLine:0:((keyLength))}" = "${key}" ] && [ "${oneLine:$((keyLength)):1}" = "=" ]; then result=${oneLine#*=} break fi done echo ${result} } 使用示例: 方法名 properties文件路径 key 。如get_value_of_properties_file /home/wja/test.properties server.port
总结
到此这篇关于shell脚本如何读取properties文件中值的文章就介绍到这了,更多相关shell读取properties文件值内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
相关内容
- shell脚本配合zabbix实现tomcat的故障自愈功能_linux shell_
- Shell内置命令教程之alias和echo_linux shell_
- Shell内置命令之exit的语法与实例_linux shell_
- shell脚本批量创建用户的方法小结_linux shell_
- 使用shell脚本判断文件后缀的方法实例_linux shell_
- Shell命令中的特殊替换、模式匹配替换、字符串提取和替换的实现_linux shell_
- shell命令执行hive脚本(hive交互)_linux shell_
- Shell 命令启动Docker Container的实现_linux shell_
- shell中的curl网络请求的实现_linux shell_
- 利用shell命令删除指定的文件的方法_linux shell_
