小编Aru*_*noy的帖子

从配置文件访问分段变量

我有一个包含部分数据的配置文件,如下所述。使用 shell 脚本访问每个变量。我正在使用 sed 命令来实现这一点,现在我面临一个问题,比如如果我忘记配置一个变量示例:名称 [APP1] 它将采用 [APP2] 名称。
配置文件:

[APP1]
name=Application1
StatusScript=/home/status_APP1.sh
startScript=/home/start_APP1.sh
stopScript=/home/stop_APP1.sh
restartScript=/home/restart.APP1.sh

[APP2]
name=Application2
StatusScript=/home/status_APP2.sh
startScript=/home/start_APP2.sh
stopScript=/home/stop_APP2.sh
restartScript=/home/restart.APP2.sh
logdir=/log/APP2/
.
.
.
.
.
[APPN]
name=ApplicationN
StatusScript=/home/status_APPN.sh
startScript=/home/start_APPN.sh
stopScript=/home/stop_APPN.sh
restartScript=/home/restart.APPN.sh
logdir=/log/APPN
Run Code Online (Sandbox Code Playgroud)

shell 命令使用:

sed -nr "/^\[APP1\]/ { :l /^name[ ]*=/ { s/.*=[ ]*//; p; q;}; n; b l;}"
Run Code Online (Sandbox Code Playgroud)

有没有办法解决这个问题,如果某个部分下没有配置某个变量,则通过 null 或 0 作为变量值。

scripting bash sed shell-script

6
推荐指数
1
解决办法
7114
查看次数

标签 统计

bash ×1

scripting ×1

sed ×1

shell-script ×1