PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, September 21, 2022

[FIXED] How can I delete hosts from my host file using bash?

 September 21, 2022     bash, sh, virtualhost     No comments   

Issue

I'm trying to automate adding and removing domains. I know that there are scripts around that do this but I want to learn more bash scripting and not execute code that I don't fully understand.

Currently I'm trying to grab the domains in my hosts file into a select list then execute the delete code to remove that line. How do I read only the lines in my hosts file that contain host names and create a select list to run the delete command on?

I've gotten the script to the point where it reads the hosts file and echos it line by line.

#!/bin/bash
### Set Language
TEXTDOMAIN=virtualhost

echo "What would you like to do?"
select choice in "Add a domain" "Delete a domain" "Quit"; do
    case $choice in
        "Add a domain" ) newDomain;break;;
        "Delete a domain" ) delDomain;break;;
        "Quit" ) echo "Goodbye!"; break;;
    esac
done

function newDomain () {
    echo "Pick a name for your new domain?"
    read domain
    re="^[-a-zA-Z0-9\.]+$"
    if ! [[ $domain =~ $re ]]; then
        echo "" 
        echo 'Only numbers, letters, hyphens and periods allowed' >&2
        read -p "Do you wish to try again (Y/N)? " -n 1 -r
        echo ""
        if [[ $REPLY =~ ^[Yy]$ ]]
            then
            newDomain
        else
            echo "Goodbye!"
            break
        fi;
    else
        echo "proceed to ask for root folder name then create conf file and enable site."
    fi;
}

function delDomain () {
    while read LINE; do
    # do something with $LINE
    echo "Line: $LINE"
    done < /etc/hosts
}  

hosts file example

127.0.0.1   localhost
127.0.1.1   Main-Server
127.0.1.1   www.site-a.com site-a.com
127.0.1.1   www.siteb.com siteb.com
127.0.1.1   www.sitec.com sitec.com
# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

I'd also like to remove the "ip", "www." ".com" and redundant aliases so the list looks like the following

Select a domain to delete.
1) site-a
2) siteb
3) sitec

I thought about using a separate configuration file to define domains but it seemed like overkill for what I was trying to do.


Solution

Essentially, you have to rewrite the file, filtering out the line you want to delete during the writing process. Something like

# untested
delDomain () {
    toDelete=$1
    tmp=$(mktemp)
    while read line; do
        if [[ $line != *$toDelete* ]]; then
            printf "%s\n" "$line"
        fi
    done < /etc/hosts > "$tmp" && mv "$tmp" /etc/hosts
}

delDomain example.com


Answered By - chepner
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing