You can create a shell function like this:
printSection()
{
section="$1"
found=false
while read line
do
[[ $found == false && "$line" != "[$section]" ]] && continue
[[ $found == true && "${line:0:1}" = '[' ]] && break
found=true
echo "$line"
done
}
You can then use printSection like a command, and pass in the section as a parameter like:
printSection APP2
To get your parameter, you can use a much simpler sed now, like:
printSection APP2 | sed -n 's/^name=//p'
This will be operating on stdin and writing to stdout. So if your example config file were called named /etc/application.conf, and you wanted to store the name of APP2 in a variable app2name, you could write this:
app2name=$(printSection APP2 | sed -n 's/^name//p/' < /etc/applications.conf)
Or, you could build the parameter part into the function and skip sed altogether, like this:
printValue()
{
section="$1"
param="$2"
found=false
while read line
do
[[ $found == false && "$line" != "[$section]" ]] && continue
[[ $found == true && "${line:0:1}" = '[' ]] && break
found=true
[[ "${line%=*}" == "$param" ]] && { echo "${line#*=}"; break; }
done
}
Then you would assign your var like this:
app2name=$(printValue APP2 name < /etc/applications.conf)
Best Answer
Using the del function in jq:
If you wanted to pass the app name as a shell variable to jq you can use the
--arg
option: