使用shell脚本将行附加到/ etc / hosts文件

时间:2021-09-29 15:28:43

I have a new Ubuntu 12.04 VPS. I am trying to write a setup script that completes an entire LAMP installation. Where I am having trouble is appending a line to the /etc/hosts file. My current hosts file looks like this:

我有一个新的Ubuntu 12.04 VPS。我正在尝试编写一个完成整个LAMP安装的安装脚本。我遇到问题的地方是在/ etc / hosts文件中附加一行。我当前的hosts文件如下所示:

127.0.0.1       localhost Venus

# 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 would like it to look like this:

我希望它看起来像这样:

127.0.0.1       localhost Venus
192.241.xx.xx  venus.example.com venus

# 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 have tried a variety of sed commands using the append (\a) command. For some reason Ubuntu either just echoes the contents of the hosts file in terminal or does nothing at all. How would I properly inject the second line into the file with a bash script?

我使用append(\ a)命令尝试了各种sed命令。出于某种原因,Ubuntu要么只是回显终端中hosts文件的内容,要么根本不做任何事情。如何使用bash脚本正确地将第二行注入文件?

6 个解决方案

#1


27  

Make sure to use the -i option of sed.

确保使用sed的-i选项。

-i[SUFFIX], --in-place[=SUFFIX]
  edit files in place (makes backup if extension supplied)

sed -i "2i192.241.xx.xx  venus.example.com venus" /etc/hosts

Otherwise,

除此以外,

echo "192.241.xx.xx  venus.example.com venus" >> /etc/hosts

would append the line at the end of the file, which could work as you expect.

将该行附加到文件的末尾,这可以按预期工作。

#2


13  

Insert/Update Entry

If you want to programmatically insert/update a hosts entry using bash, here's a script I wrote to do that:

如果你想使用bash以编程方式插入/更新主机条目,这是我写的一个脚本:

#!/bin/bash

# insert/update hosts entry
ip_address="192.168.x.x"
host_name="my.hostname.example.com"
# find existing instances in the host file and save the line numbers
matches_in_hosts="$(grep -n $host_name /etc/hosts | cut -f1 -d:)"
host_entry="${ip_address} ${host_name}"

echo "Please enter your password if requested."

if [ ! -z "$matches_in_hosts" ]
then
    echo "Updating existing hosts entry."
    # iterate over the line numbers on which matches were found
    while read -r line_number; do
        # replace the text of each line with the desired host entry
        sudo sed -i '' "${line_number}s/.*/${host_entry} /" /etc/hosts
    done <<< "$matches_in_hosts"
else
    echo "Adding new hosts entry."
    echo "$host_entry" | sudo tee -a /etc/hosts > /dev/null
fi

The script is intended for use with OS X but would work on linux as well with minor tweaking.

该脚本旨在与OS X一起使用,但也可以在linux上进行微调。

#3


5  

If your in mac or you need sudo permission to this try this:

如果您在Mac中或需要sudo权限,请尝试以下操作:

sudo -- sh -c -e "echo '192.34.0.03   subdomain.domain.com' >> /etc/hosts";

It will still ask you for password.

它仍然会要求您输入密码。

#4


3  

echo "127.0.0.1 localhost `hostname`">./temp_hosts
echo "192.241.xx.xx  venus.example.com">>./temp_hosts
cat /etc/hosts |tail -n +2 >>./temp_hosts
cat ./temp_hosts > /etc/hosts
rm ./temp_file

#5


1  

I should point out that sed (the stream editor) is not actually intended for editing files, although it can be used to do that. (Standard sed doesn't have a built-in mechanism for writing to other than standard output.) A more appropriate tool would be ed.

我应该指出sed(流编辑器)实际上不是用于编辑文件,尽管它可以用来做。 (标准sed没有用于写入标准输出以外的内置机制。)将使用更合适的工具。

The following ed script says "find the line containing the (admittedly sloppy) regular expression /127.0.0.1/ and append at the next line." (The lone period tells ed to stop appending.)

下面的ed脚本说“找到包含(公认的马虎)正则表达式/127.0.0.1/的行,并在下一行附加。” (唯一的一段时间告诉ed停止追加。)

ed /etc/hosts <<-'EOF'
    /127.0.0.1/a
    192.241.xx.xx  venus.example.com
    .
    wq
EOF

That said, you can really just append this line to the end of your /etc/hosts file very trivially:

也就是说,您可以非常简单地将此行附加到/ etc / hosts文件的末尾:

echo '192.241.xx.xx  venus.example.com' >> /etc/hosts

#6


0  

you can use sed, like:

你可以使用sed,如:

sed '/Venus/ a\
192.241.xx.xx venus.example.com venus' /etc/hosts

sed'/ Venus / a \ 192.241.xx.xx venus.example.com venus'/ etc / hosts

#1


27  

Make sure to use the -i option of sed.

确保使用sed的-i选项。

-i[SUFFIX], --in-place[=SUFFIX]
  edit files in place (makes backup if extension supplied)

sed -i "2i192.241.xx.xx  venus.example.com venus" /etc/hosts

Otherwise,

除此以外,

echo "192.241.xx.xx  venus.example.com venus" >> /etc/hosts

would append the line at the end of the file, which could work as you expect.

将该行附加到文件的末尾,这可以按预期工作。

#2


13  

Insert/Update Entry

If you want to programmatically insert/update a hosts entry using bash, here's a script I wrote to do that:

如果你想使用bash以编程方式插入/更新主机条目,这是我写的一个脚本:

#!/bin/bash

# insert/update hosts entry
ip_address="192.168.x.x"
host_name="my.hostname.example.com"
# find existing instances in the host file and save the line numbers
matches_in_hosts="$(grep -n $host_name /etc/hosts | cut -f1 -d:)"
host_entry="${ip_address} ${host_name}"

echo "Please enter your password if requested."

if [ ! -z "$matches_in_hosts" ]
then
    echo "Updating existing hosts entry."
    # iterate over the line numbers on which matches were found
    while read -r line_number; do
        # replace the text of each line with the desired host entry
        sudo sed -i '' "${line_number}s/.*/${host_entry} /" /etc/hosts
    done <<< "$matches_in_hosts"
else
    echo "Adding new hosts entry."
    echo "$host_entry" | sudo tee -a /etc/hosts > /dev/null
fi

The script is intended for use with OS X but would work on linux as well with minor tweaking.

该脚本旨在与OS X一起使用,但也可以在linux上进行微调。

#3


5  

If your in mac or you need sudo permission to this try this:

如果您在Mac中或需要sudo权限,请尝试以下操作:

sudo -- sh -c -e "echo '192.34.0.03   subdomain.domain.com' >> /etc/hosts";

It will still ask you for password.

它仍然会要求您输入密码。

#4


3  

echo "127.0.0.1 localhost `hostname`">./temp_hosts
echo "192.241.xx.xx  venus.example.com">>./temp_hosts
cat /etc/hosts |tail -n +2 >>./temp_hosts
cat ./temp_hosts > /etc/hosts
rm ./temp_file

#5


1  

I should point out that sed (the stream editor) is not actually intended for editing files, although it can be used to do that. (Standard sed doesn't have a built-in mechanism for writing to other than standard output.) A more appropriate tool would be ed.

我应该指出sed(流编辑器)实际上不是用于编辑文件,尽管它可以用来做。 (标准sed没有用于写入标准输出以外的内置机制。)将使用更合适的工具。

The following ed script says "find the line containing the (admittedly sloppy) regular expression /127.0.0.1/ and append at the next line." (The lone period tells ed to stop appending.)

下面的ed脚本说“找到包含(公认的马虎)正则表达式/127.0.0.1/的行,并在下一行附加。” (唯一的一段时间告诉ed停止追加。)

ed /etc/hosts <<-'EOF'
    /127.0.0.1/a
    192.241.xx.xx  venus.example.com
    .
    wq
EOF

That said, you can really just append this line to the end of your /etc/hosts file very trivially:

也就是说,您可以非常简单地将此行附加到/ etc / hosts文件的末尾:

echo '192.241.xx.xx  venus.example.com' >> /etc/hosts

#6


0  

you can use sed, like:

你可以使用sed,如:

sed '/Venus/ a\
192.241.xx.xx venus.example.com venus' /etc/hosts

sed'/ Venus / a \ 192.241.xx.xx venus.example.com venus'/ etc / hosts