網站首頁 編程語言 正文
如下面代碼所示的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
總結
原文鏈接:https://blog.csdn.net/monarch91/article/details/123010625
相關推薦
- 2022-05-13 linq中的限定操作符_實用技巧
- 2022-05-12 Android 截屏實現、屏幕截圖、MediaProjection、ImageReader
- 2022-09-25 git提交代碼版本沖突問題
- 2022-08-10 WPF中圖像處理的方法介紹_C#教程
- 2022-11-16 python壓縮和解壓縮模塊之zlib的用法_python
- 2021-12-16 Warning: Can‘t perform a React state update on an
- 2022-10-12 字節封裝React組件手機號自動校驗格式FormItem_React
- 2022-10-22 python?中的?super詳解_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支