0

I'm trying to write a simple script to add some configuration at the top of of the file, and that's how I do this:

 #! /bin/bash

 sudo apt-get install monit
 # BELOW IS THE CODE THAT I'M INTERESTING TO CHANGE 
 echo '
 set eventqueue basedir /etc/monit/eventqueue/ slots 1000
 set mmonit http://monit:monit@xxx.xxx.xxx.xxx:8080/collector
 set httpd port 2812 and use address ec2-xxx.xxx.xx.xx.com 
     allow localhost
     allow 0.0.0.0/0.0.0.0
     allow admin:swordfish
 ' | sudo tee -a /etc/monit/monitrc_tmp
sudo cat /etc/monit/monitrc >> /etc/monit/monitrc_tmp
sudo rm /etc/monit/monitrc
sudo mv /etc/monit/monitrc_tmp /etc/monit/monitrc
# UP TO THIS POINT
sudo sed -i 's/set daemon 120/set daemon 20/' /etc/monit/monitrc
exit 0

As you can see I 'm trying to add some configuration at the top of the file. And just want to know is there any flag or command that will help me do this without creating a tmp file.

4

2 回答 2

3

看起来像一个案例,sed -i因为你在 Linux 上。此外,由于这是系统管理工作,请保留备份。

sudo sed -i.bak -e '1i\
set eventqueue basedir /etc/monit/eventqueue/ slots 1000\
set mmonit http://monit:monit@xxx.xxx.xxx.xxx:8080/collector\
set httpd port 2812 and use address ec2-xxx.xxx.xx.xx.com\
    allow localhost\
    allow 0.0.0.0/0.0.0.0\
    allow admin:swordfish
' /etc/monit/monitrc

这表示'在第 1 行之前插入以下行'......并且这些行继续到并包括最后没有反斜杠的行。

set daemon即使您说它超出范围,您也可以编辑该行:

sudo sed -i.bak -e '1i\
set eventqueue basedir /etc/monit/eventqueue/ slots 1000\
set mmonit http://monit:monit@xxx.xxx.xxx.xxx:8080/collector\
set httpd port 2812 and use address ec2-xxx.xxx.xx.xx.com\
    allow localhost\
    allow 0.0.0.0/0.0.0.0\
    allow admin:swordfish
s/set daemon 120/set daemon 20/
' /etc/monit/monitrc
于 2013-08-26T17:32:06.527 回答
2

这应该将这些行添加到文件的顶部。

  echo -e " set eventqueue basedir /etc/monit/eventqueue/ slots 1000 \n
  set mmonit http://monit:monit@xxx.xxx.xxx.xxx:8080/collector \n
  set httpd port 2812 and use address ec2-xxx.xxx.xx.xx.com \n
      allow localhost \n
      allow 0.0.0.0/0.0.0.0 \n
      allow admin:swordfish \n 
  $(cat /etc/monit/monitrc)" > /etc/monit/monitrc

 sudo sed -i 's/set daemon 120/set daemon 20/' /etc/monit/monitrc
 exit 0
于 2013-08-26T17:35:50.273 回答