Append line to /etc/hosts file with shell script

前端 未结 7 1741
春和景丽
春和景丽 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:13

    Make sure to use the -i option of sed.

    -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.

    0 讨论(0)
提交回复
热议问题