2016-07-08 7 views
0

EnableSendfileを見つけたいが、その文字列を含む行ではなく、行が "#"(コメント行ではない)で始まる場合はif httpd.conf私はその値をオフにリセットしたいと思った。ファイル内の文字列または文字列を検索して置換する

私はこれを思いついた。 (私は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

使用sed

sed 's/^EnableSendfile on/EnableSendfile off/' 

^特殊文字は、行の先頭にマッチし、そのコマンドが#で始まる行を変更しません。

+0

hhow変数をsed、sincで使用することはできますか?後で配列項目を使用してキーとその値をループするため、プレーンな文字列の代わりに変数を使用する必要があります。 – Vagho

+0

変数に特殊文字二重引用符で ''/$ keyword on/$ keyword off/''を使うことができます。それ以外の場合は、より複雑なソリューションが必要です。 – choroba

+0

ok .iはできましたが、明らかに画面上にファイルを表示しているだけで保存しているわけではありません。保存する方法や元のファイルを置き換える方法はありますか? – Vagho

関連する問題