LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-09-2019, 05:55 AM   #16
kwoot
LQ Newbie
 
Registered: Nov 2019
Posts: 3

Rep: Reputation: Disabled
I think this is a much better solution


Hi,

So I had exactly the same problem.
And I found a hint on the web called "Augeas".
There is a website, but the docs are, well, way too complicated, with just a few examples.

So I started trying stuff out and found the following usage examples.
I hope they are of benefit to you all:

$ augtool print /files/etc/apache2/sites-available/mydomain.tld.conf/VirtualHost/
$ augtool match /files/etc/apache2/sites-available/mydomain.tld.conf/VirtualHost[*]/directive[*] DocumentRoot
/files/etc/apache2/sites-available/mydomain.tld.conf/VirtualHost[2]/directive[3]

$ augtool print /files/etc/apache2/sites-available/mydomain.tld.conf/VirtualHost[2]/directive[3]
/files/etc/apache2/sites-available/mydomain.tld.conf/VirtualHost[2]/directive[3] = "DocumentRoot"
/files/etc/apache2/sites-available/mydomain.tld.conf/VirtualHost[2]/directive[3]/arg = ""/srv/www/mydomain.tld/wordpress""

$ augtool print /files/etc/apache2/sites-available/mydomain.tld.conf/VirtualHost[2]/directive[3]/arg
$/files/etc/apache2/sites-available/mydomain.tld.conf/VirtualHost[2]/directive[3]/arg = ""/srv/www/mydomain.tld/wordpress""

$ augtool match /files/etc/apache2/sites-available/mydomain.tld.conf/VirtualHost[*]/directive[*] SSLCertificateFile
/files/etc/apache2/sites-available/mydomain.tld.conf/VirtualHost[2]/directive[14]

$ augtool print /files/etc/apache2/sites-available/mydomain.tld.conf/VirtualHost[2]/directive[14]
/files/etc/apache2/sites-available/mydomain.tld.conf/VirtualHost[2]/directive[14] = "SSLCertificateFile"
/files/etc/apache2/sites-available/mydomain.tld.conf/VirtualHost[2]/directive[14]/arg = "/etc/letsencrypt/live/mydomain.tld/fullchain.pem"


Regards,
Jeroen Baten
 
1 members found this post helpful.
Old 11-09-2019, 01:17 PM   #17
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,266
Blog Entries: 24

Rep: Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195
Welcome to LQ kwoot!

Problems are rarely exactly the same, and without reading through the entire thread others cannot tell how your examples relate to the thread subject, or if they indeed do so.

Trying stuff out, as you say, provides no useful information and is not a basis for presenting those examples as "a better solution" to your unspecified problem.

Please review the Site FAQ for guidance in posting to this forum, and general forum usage.
 
Old 11-10-2019, 03:02 AM   #18
kwoot
LQ Newbie
 
Registered: Nov 2019
Posts: 3

Rep: Reputation: Disabled
reply

I think I understand your situation. You see a fresh account who post immediately and think it is another spammer. I get it. I do.
The thing is, if you would google on how to script and getting data out of Apache conf files this thread is about the only worthwhile thing you'll find.
So I did more research and found a tool that is ASAIK not very well known to the general public. There is a reason for that. Basically the docs could contain more examples.

So I figured some stuff out and posted it here for the next poor bastard who googles this problem will likely also turn up here.

But you'r right. Maybe I should have posted this answer on stackoverflow instead.

Regards,
Jeroen

Quote:
Originally Posted by astrogeek View Post
Welcome to LQ kwoot!

Problems are rarely exactly the same, and without reading through the entire thread others cannot tell how your examples relate to the thread subject, or if they indeed do so.

Trying stuff out, as you say, provides no useful information and is not a basis for presenting those examples as "a better solution" to your unspecified problem.

Please review the Site FAQ for guidance in posting to this forum, and general forum usage.
 
Old 11-10-2019, 07:25 AM   #19
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
this seems to have been a failure to follow the rule
"If you are going to use bash, use bash"


all that messing about with sed grep awk isn't needed
Code:
#!/bin/bash
while read -a line
do
  case ${line[0]} in
      \<VirtualHost)
        printf "vhost=%s\n" ${line[1]:0:(-1)}
        ;;
    ServerName)
        printf "domainName=%s\n" ${line[1]}
        #printf "domainName=%s\n" ${line[1]//.}
        # there was mention of removing dots
        ;;
    DocumentRoot)
        printf "PdocumentRoot=%s\n" ${line[1]}
        ;;
    \#\#) 
        [[ ${line[1]} == User ]] \
          && printf "cpanelUser=%s\n" ${line[2]}
        ;;
    \</VirtualHost\>)
        printf "\n"
        ;;
  esac
done < SampleFromPost6.conf


#
i did only skim the thread
the input data is from post 6
I'm uncertain if it is malformed,

this is the output
Code:
vhost=192.168.1.20:80
domainName=example.com
PdocumentRoot=/home/example/public_html
cpanelUser=example
vhost=192.168.1.20:80
domainName=app.example.com
PdocumentRoot=/home/example/public_html/app
cpanelUser=example

vhost=192.168.1.21:443
domainName=store.domain.com
PdocumentRoot=/home/username/public_html/store
cpanelUser=username



#
 
Old 11-11-2019, 07:55 AM   #20
kwoot
LQ Newbie
 
Registered: Nov 2019
Posts: 3

Rep: Reputation: Disabled
Wow, this is some next level bash scripting!
And a much better solution than the one I found.
Thanks!
 
Old 11-11-2019, 09:59 PM   #21
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Quote:
Originally Posted by kwoot View Post
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
 
  


Reply



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
bash script position parameter does is not parsing '$1' junust Programming 5 04-08-2010 07:50 PM
Parsing unflagged parameters in bash script Calab Programming 3 04-07-2010 03:32 AM
bash script - parsing optional parameters yitzle Programming 5 02-17-2008 11:16 AM
Parsing a File in a Bash Script TGWDNGHN Programming 4 12-02-2005 02:38 PM
bash script, parsing email addresses kepler Programming 6 01-26-2004 06:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:16 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