Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8569011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:14:11+00:00 2026-06-11T18:14:11+00:00

I wrote a script to automatically add a ServerAlias to an Apache configuration file,

  • 0

I wrote a script to automatically add a ServerAlias to an Apache configuration file, then restart Apache, with a rather remedial understanding of sed and tr and probably shell scripting in general. This is what I came up with:

cp /etc/httpd/sites/site.conf tmphost &&
sed s/ServerName\ site.com/ServerName\ site.com^#ServerAlias\ sub.site.com/ \
        tmphost |
    tr '^#' '\n\t' >/etc/httpd/sites/site.conf &&
rm -f tmphost &&
apachectl restart

Basically I’m creating a copy, replacing the ServerName line with itself + the new alias, using tr to put in the newline and tab (sed was being weird about that?), overwriting the old configuration file, deleting the copy, then restarting Apache.

It works, but doesn’t really make mama proud, if you know what I mean. Any ideas on how to clean that up?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:14:12+00:00Added an answer on June 11, 2026 at 6:14 pm

    If you want to add a ServerAlias line after ServerName line, why not use the ‘append’ command to add a line:

    sed '/ServerName site.com/a\
        ServerAlias sub.site.com' tmphost >/etc/httpd/sites/site.conf
    

    There’s a ‘tab’ at the start of the extra line, which you seem to need.

    If you have GNU sed, you can do the edit in-place with the -i option and without the temporary file (but don’t use the -i option if the file you are editing has multiple hard links, and probably not it if it is a symlink, either; see the comments below from William Pursell).

    Note that you should wrap the code in:

    trap "rm -f tmphost; exit 1" 0 1 2 3 13 15
    ...script manipulating tmphost...
    rm -f tmphost
    trap 0
    

    so that the temporary file is not left around if the shell exits because of a signal. Of course, if you have an in-place alter, you don’t need the temporary file at all.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a script parsing a .csv file in groovy using tokenize, which ended
I wrote this script which counts occurrences of particular pattern in a given file.
I wrote a script using Ruby to click on the screen automatically on Windows's
I have a small python script which serves as a configuration file. The structure
I wrote this script to change the usb keyboard layout automatically on plug-in import
I while back I wrote a shell script that automatically runs a python script
I've wrote a script that will be used to release the new pages automatically
I need to be able to write a script to automatically connect mysql in
I wrote some script in a site. The script makes a new spreadsheet and
I wrote a script that uses xcodebuild to generate an AdHoc build of an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.