Bash: Extracting the Configuration of a Single Apache Virtual Host

Starting a series of posts with interesting Bash puzzles.

A puzzle about how to cut out part of an Apache config file.

Task: An Apache2 server running on CentOS v.6 has about 50 sites configured. Additional condition: they’re all described across at least three config files (their location unknown). You need to write a Bash script that pulls the settings for 4 sites out of the three config files and saves them either into separate files or into one single file. List of sites:

  • website1.com
  • website2.net
  • website3.info
  • website4.org

I figure there can be two types of readers:

  1. Those looking for the puzzle.
  2. Those looking for the solution.

For the second type of reader, here’s the script:

Unfold me



If you change the filename extracted_host.conf to $website.conf, each site’s config will get saved into its own separate file.