I'm trying to create a bash script that will read the samba config, loop through the different "sections" and act if a variable is set
Here is an example config:
[global]
workgroup = METRAN
encrypt passwords = yes
wins support = yes
log level = 1
max log size = 1000
read only = no
[homes]
browsable = no
map archive = yes
[printers]
path = /var/tmp
printable = yes
min print space = 2000
[music]
browsable = yes
read only = yes
path = /usr/local/samba/tmp
[pictures]
browsable = yes
read only = yes
path = /usr/local/samba/tmp
force user = www-data
Here is kinda what I want to do (I know this syntax a real language, but it should give you the idea:
#!/bin/sh
#
CONFIG='/etc/samba/smb.conf'
sections = magicto $CONFIG #array of sections
foreach $sections as $sectionname #loop through the sections
if $sectionname != ("homes" or "global" or "printers")
if $force_user is-set
do something with $sectionname and with $force_user
endif
else
do something with $sectionname
endelse
endif
endforeach
This will read each section and get key,value pair.
#!/bin/bash
CONFIG='/etc/samba/smb.conf'
for i in homes music; do
echo "$i"
sed -n '/\['$i'\]/,/\[/{/^\[.*$/!p}' $CONFIG | while read -r line; do
printf "%-15s : %s\n" "${line%?=*}" "${line#*=?}"
done
done
Output
homes
browsable : no
map archive : yes
music
browsable : yes
read only : yes
path : /usr/local/samba/tmp
Explanation
sed -n '/\['$i'\]/,/\[/{/^\[.*$/!p}'
1) /\['$i'\]/,/\[/
Matches the section name between []
until the next [
2) {/^\[.*$/!p}
Matches the start of line ^
followed by [
and zero or more characters .*
until end of line $
and if matches don't print !p
${line%?=*}
Trim string from the end (right) until first =
and any char ?
${line#*=?}
Trim string from the start (left) until first =
and any char ?