Quote:
Originally Posted by kwoot
Wow, this is some next level bash scripting!
And a much better solution than the one I found.
Thanks!
|
well, not really
looking back, post 7 has a much better solution using awk
https://www.linuxquestions.org/quest...3/#post5428871
and a clumsy awk I came up with
Code:
#!/usr/bin/awk -f
{
if ( $1 == "<VirtualHost" ) {
sub(/>/,"",$2)
printf "vhost=%s\n",$2
} else if ( $1 == "ServerName" ) {
printf "domainName=%s\n",$2
} else if ( $1 == "DocumentRoot" ) {
printf "PdocumentRoot=%s\n",$2
} else if ( $1 == "##" && $2 == "User" ) {
printf "cpanelUser=%s\n",$3
} else if ( $1 == "</VirtualHost>" ) {
printf "\n"
}
}
#
the case statement in the bash script is really a bunch of
Code:
if [[ $var =~ <RE> ]]
then
.. stuff ..
elif [[ $var =~ <RE> ]]
then
.. different stuff
...
which is what that awk is doing
now, the while read -a
is basically putting each line into an array ( I called it line, since I never remember the default array name )
which, when you think about it is what awk does, reads each line into array
The awk is slightly faster
but the difference is negligible here ( 1 or 2 milliseconds )
if you want to learn more about bash
https://mywiki.wooledge.org/BashGuide
now sed
Code:
#!/bin/sed -nEf
{
s/^[[:blank:]]*<VirtualHost[[:blank:]]+([[:graph:]]+)>/vhost=\1/p
s/^[[:blank:]]*ServerName[[:blank:]]+([[:graph:]]+)/domainName=\1/p
s/^[[:blank:]]*DocumentRoot[[:blank:]]+([[:graph:]]+)/PdocumentRoot=\1/p
s/^[[:blank:]]*##[[:blank:]]+User[[:blank:]]+([[:graph:]]+).*/cpanelUser=\1/p
s/^[[:blank:]]*<\/VirtualHost>//p
}
#
about the same speed as the awk
grep is not really suitable , but I could possibly hack something together with grep -o