我正在尝试编写一个简单的脚本来在文件顶部添加一些配置,这就是我这样做的方式:
#! /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
如您所见,我正在尝试在文件顶部添加一些配置。只是想知道是否有任何 flag
或 command
可以帮助我在不创建 tmp
文件的情况下执行此操作。
最佳答案
看起来像 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
关于linux - 编辑不带临时文件的配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18449552/