2016-07-08 2 views
0

EnableSendfile 찾을 수 있지만이 줄을 포함하는 줄을 "#"(주석이없는 줄) 시작하는이 파일 httpd.conf 있습니다. if 내가 그 값을 꺼 놓고 싶다.찾기 및 바꾸기 파일에서 문자열 또는 bash에 바꿉니다

나는 이것을 생각해 냈습니다. (저는 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

관련 문제