有时候我们需要脚本程序能保存一些设置,以便于下次运行的时候不用再做修改。即在启动的时候能够从文件(比如用ini文件)读取数据保存到变量里。
(会逐步把之前写bash脚本、QT、C++,ros2,python中用到的,学到的一些小技巧整理出来,分到不同的帖子里,备忘备查)
#!/bin/bash
##All right reserved Version 3.0 @ 18/11/2022 LYX
inifilename="/tmp/rtUD.ini"
if test -e $inifilename #判断文件是否存在
then
SetStr=`cat $inifilename|awk '{printf("curType=%s; curSelectType=%s; curLimitRows=%s; curDisplayType=%s; curShowType=%s; curLangSet=%s",$1,$2,$3,$4,$5,$6)}'` #读取ini文件,用awk 变成字符串赋值语句
eval $SetStr #eval 将字符串作为语句执行
else #ini文件不存在,用缺省值
curType=1 #1:
curSelectType=1 #1: Biggest UDSN 2: Latest report3: Today Most Latest
curLimitRows=3 # 0 :All 1-20 limit rows
curDisplayType=1 #1 raw data 2 Key info and parse some fields
curShowType=1 #1 show on screen, 2 save to csv
curLangSet=1 #1 English 2 Chinese
fi
使用完设置,把一些做过修改的设置保存下来
echo "${curType} " "${curSelectType} " "${curLimitRows} " "${curDisplayType} " "${curShowType} " "${curLangSet} " >$inifilename
需要500关注,拜托点个关注,谢谢

本文介绍了一个Bash脚本示例,该脚本能够在启动时从INI文件读取配置设置,并在运行过程中更新这些设置。通过使用cat和awk命令组合,脚本能够有效地解析INI文件并将其内容转换为Shell变量。
7063

被折叠的 条评论
为什么被折叠?



