我在找一个小剧本的帮助。
我想搜索所有与
/usr/local/directadmin/data/users/*/httpd.conf用于字符串
centralized.log如果字符串不存在于文件中,我想在其中插入2行。
目前,我有以下脚本:
#!/bin/bash
if ! grep -q centralized.log /usr/local/directadmin/data/users/*/httpd.conf ; then
sed -i '33iCustomLog /var/log/centralized.log combined' /usr/local/directadmin/data/users/*/httpd.conf && sed -i '65iCustomLog /var/log/centralized.log combined' /usr/local/directadmin/data/users/*/httpd.conf
fi目前,如果没有在任何文件中找到字符串,则将行插入到所有文件中,如果在至少一个文件中找到字符串,则不会发生任何事情。我是否可以将行添加到字符串不存在的所有文件中?
发布于 2015-08-09 18:46:19
使用循环:
#!/bin/bash
for file in /usr/local/directadmin/data/users/*/httpd.conf ; do
if ! grep -q centralized.log "$file" ; then
sed -i '33iCustomLog /var/log/centralized.log combined' "$file"
sed -i '65iCustomLog /var/log/centralized.log combined' "$file"
fi
done发布于 2015-08-10 19:29:31
用GNU awk:
awk -v RS='^$' -v ORS= -i inplace '
{ print }
!/centralized\.log/ {
print "33iCustomLog /var/log/centralized.log combined"
print "65iCustomLog /var/log/centralized.log combined"
}
' /usr/local/directadmin/data/users/*/httpd.confhttps://stackoverflow.com/questions/31907605
复制相似问题