Append line to /etc/hosts file with shell script

前端 未结 7 1742
春和景丽
春和景丽 2021-01-30 08:41

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/ho

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-30 09:05

    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.

    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 /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:

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

提交回复
热议问题