用于删除文件中的单词的Shell脚本

时间:2014-09-17 06:01:30

标签: linux bash shell command-line

任何人都可以帮我开发以下脚本吗?

我在/etc/httpd/httpd.conf文件中有以下条目

<VirtualHost 192.168.1.181:80> 
 DocumentRoot /var/www/html/  
 ServerName example.com 
 </VirtualHost>"

如果有人进入输入example.com,我需要从同一个文件/etc/httpd/httpd.conf中完全删除example.com的相同虚拟主机条目。我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

这是一个bash脚本,它将删除包含与作为程序第二个参数提供的VirtualHost匹配的domain的任何domain块。脚本使用是:

./scriptname /path/to/httpd.conf somedomain.com

操作很简单。它将通过现有的httpd.conf读取,在/ tmp中创建一个临时的httpd.conf。它读取httpd.conf以查找任何VirtualHost标记,然后在数组中缓冲与它们关联的所有行。它会测试somedomain.com块中是否找到VirtualHost。如果找到,则不会写入新文件。如果未找到,则VirtualHost块的所有行都将保持不变。与VirtualHost块无关的任何其他行都只是写入新文件。解析后,通过diff比较新的/旧的httpd.conf文件。如果它们不匹配,则将新的httpd.conf写入系统httpd.conf位置。

脚本在下面评论。如果您还有其他问题,请与我联系:

#!/bin/bash

# this file must be run as root
test "$UID" = 0 || {
    printf "\n  Error, insufficient privileges. root user required, user '%s' won't work.\n\n" "$USER"
    exit 1
}

## declare needed variables and flags
declare -a tmp                      # temp array to hold lines between <VirtualHost tags
declare -i loop=0                   # flag to loop over all line in <VirtualHost blocks
declare -i found=0                  # flag indicating domain to delete is found
tmstamp="$(date +%s)"               # unique timestamp for backup of httpd.conf
domain="${2:-example.com}"          # give domain to find as 2nd arg on command line
htfile="${1:-/etc/httpd/conf/httpd.conf}" # confirm path/filename for your setup
tmpfile="/tmp/httpd.conf.tmp"       # temp file to write remaining httpd.conf lines to
:> "$tmpfile"                       # truncate tmpfile

## backup httpd.conf, exit on err
cp -a "${htfile}" "${htfile}.$tmstamp" || {
    printf "\n Error, failed to make backup of httpd.conf.\n\n"
    exit 2
}

## NOTE: do not unset IFS, leave at default
#  read all lines in httpd.conf
while read -r line || test -n "$line"; do
    if test "${line:0:12}" == "<VirtualHost" || test $loop -eq 1 ; then  # if <VirtualHost found
        loop=1                                      # set loop flag to 1 to continue looping
        tmp+=( "$line" )                            # while looping add each line to tmp array
        test "${line##* }" == "$domain" && found=1  # if wanted domain found, set found flag=1
        if test "$line" == "</VirtualHost>" ; then  # if closing </VirtualHost tag found
            loop=0                                  # reset loop to 0
            if test "$found" -eq 1 ; then           # if 'found', just reset found flag (don't write)
                found=0
            else                                    # otherwise, write the VirtualHost block to file`
                for ((i=0; i<${#tmp[@]}; i++)); do
                    printf "%s\n" "${tmp[$i]}" >> "$tmpfile"
                done
            fi
            unset tmp                               # lastly - unset tmp array
        fi
    else                                            # Not in VirtualHost block, so
        printf "%s\n" "$line" >> "$tmpfile"         # output all non-interesting lines to tmpfile
    fi
done <"$htfile"

## if new and old httpd.conf files differ, copy new to old
diff -qw &>/dev/null "$htfile" "$tmpfile" || cp -a "$tmpfile" "$htfile"

rm "$tmpfile"   # remove tmpfile

exit 0

答案 1 :(得分:0)

试试这段代码。

#!/bin/bash

read -p 'Enter the domain' dom

NUM=$(cat -n /etc/httpd/httpd.conf |grep -w $dom|awk '{print $1}')
NUM1=$(($NUM-2))
NUM2=$(($NUM+1))
sed -i -e "$NUM1,$NUM2 d" /etc/httpd/httpd.conf
相关问题