LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-12-2020, 09:59 AM   #1
Basher52
Member
 
Registered: Mar 2004
Location: .SE
Distribution: Arch
Posts: 401

Rep: Reputation: 22
Question conf file parser for bash?


Hi!

Anyone in here knows a nice conf file parser for bash?
It's supposed to look like yum.conf, sections, names and it's values.
It's a bash script that's gonna read it and use its values for when it's running.

Instead of creating my own there must be plenty of these already made but I just can't find any.

//B52

UPDATE: Also if there is a conf file writer would be superb so I can write to it too

Last edited by Basher52; 08-12-2020 at 10:17 AM.
 
Old 08-12-2020, 10:24 AM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,688

Rep: Reputation: Disabled
See this post.
 
2 members found this post helpful.
Old 08-12-2020, 10:28 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,114

Rep: Reputation: 7368Reputation: 7368Reputation: 7368Reputation: 7368Reputation: 7368Reputation: 7368Reputation: 7368Reputation: 7368Reputation: 7368Reputation: 7368Reputation: 7368
if you mean the usual ini file format
here is a simple one: https://gist.github.com/splaspood/1473761 or here is a modified version with writer: https://ajdiaz.wordpress.com/2008/02...sh-ini-parser/
and here is the "most advanced" one: https://github.com/albfan/bash-ini-parser (as far as I know)
 
1 members found this post helpful.
Old 08-12-2020, 01:53 PM   #4
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
From a discussion in another forum:
Code:
# Usage: parse_config_vars <file>
# Preexisting variables will be overwritten.
parse_config_vars(){
    local line key value entry_regex
    entry_regex_quotes="^[[:blank:]]*([[:alpha:]_][[:alnum:]_]*)[[:blank:]]*=[[:blank:]]*('[^']+'|\"[^\"]+\")[[:blank:]]*(#.*)*$"
    entry_regex_loose="^[[:blank:]]*([[:alpha:]_][[:alnum:]_]*)[[:blank:]]*=[[:blank:]]*([^#]*[^#[:blank:]])*"
    while read -r line
    do
        [[ -n $line ]] || continue
        if [[ $line =~ $entry_regex_quotes ]]
        then
            value=${BASH_REMATCH[2]#[\'\"]} # strip quotes
            value=${value%[\'\"]}
        elif [[ $line =~ $entry_regex_loose ]]
        then
            value=${BASH_REMATCH[2]}
        else
            continue
        fi
        key=${BASH_REMATCH[1]}
        echo "Parsing config (new regex): ${key} = ${value}"
        declare -g "${key}"="${value}"
    done < "$1"
    return 0
}
It assigns config variables' values to bash variables of the same name IIRC, without the dangers of sourcing.
 
1 members found this post helpful.
Old 08-13-2020, 01:02 PM   #5
Basher52
Member
 
Registered: Mar 2004
Location: .SE
Distribution: Arch
Posts: 401

Original Poster
Rep: Reputation: 22
I'm gonna check all these.
Thanks all
 
  


Reply

Tags
bash, conf, ini, parser



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Read parameters from config file (file parser?) alaios Programming 8 07-09-2012 11:29 AM
LXer: Build a Log Parser with Bash LXer Syndicated Linux News 0 02-28-2011 06:41 PM
grep'ing and sed'ing chunks in bash... need help on speeding up a log parser. elinenbe Programming 4 04-22-2009 10:17 AM
checking for XML::Parser... configure: error: XML::Parser perl module is required for kornerr Linux - General 11 11-16-2008 07:24 AM
Log File Parser Program kaplan71 Linux - General 1 05-11-2005 08:55 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration