我有这个文件httpd.conf
,我希望找到EnableSendfile
而不是包含该字符串的行,但该行以"#"开头。 (不是注释行)如果发现我想将其值重置为关闭。
我试图想出这个。 (我是bash语言的新手) 但它没有用。
我想在搜索和替换命令中使用变量,因为我稍后会使用数组项来循环键及其值,因此我需要使用变量而不是普通字符串。
提前感谢您的帮助!
#!/bin/bash
SEARCH="EnableSendfile"
FILEPATH="/root/scripts/httpd.conf"
REPLACE="off"
if grep -Fq $SEARCH $FILEPATH
then
line_string=`sed -n '/^EnableSendfile/p' $FILEPATH`
result_string="${line_string/(^[^#]\w*)(?:\s(\w+))?$/$REPLACE}"
echo $result_string
else
echo "not found"
fi
#
# EnableMMAP and EnableSendfile: On systems that support it,
# memory-mapping or the sendfile syscall may be used to deliver
# files. This usually improves server performance, but must
# be turned off when serving from networked-mounted
# filesystems or if support for these functions is otherwise
# broken on your system.
# Defaults if commented: EnableMMAP On, EnableSendfile Off
#
#EnableMMAP off
EnableSendfile on
#EnableSendfile on
# Supplemental configuration
#
# Load config files in the "/etc/httpd/conf.d" directory, if any.
IncludeOptional conf.d/*.conf
答案 0 :(得分:0)
使用sed
:
sed 's/^EnableSendfile on/EnableSendfile off/'
^
特殊字符在行的开头匹配,因此该命令不会更改以#
开头的行。