【问题标题】:Shell script for deleting a word in a file用于删除文件中的单词的 Shell 脚本
【发布时间】:2014-09-17 06:01:29
【问题描述】:

谁能帮我开发以下脚本?

我在 /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 的相同虚拟主机条目。我该怎么做?

【问题讨论】:

  • 是否要删除 .. 的完整部分?
  • 是的,我想删除该虚拟主机的完整部分
  • 为什么会这样,你有什么解决办法
  • 我尝试了一些东西,但它在 bash 中并不能很好地工作,我可以在 python 中这样做,但在 bash 中我没有得到它
  • 你能用python吗

标签: linux bash shell command-line


【解决方案1】:

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

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

操作很简单。它将通读现有的 httpd.conf,在 /tmp 中创建一个临时的 httpd.conf。它读取 httpd.conf 以查找任何 VirtualHost 标记,然后将与它们关联的所有行缓存在一个数组中。它测试是否在该VirtualHost 块中找到somedomain.com。如果找到,则不会写入新文件。如果未找到,则 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

【讨论】:

  • 很高兴它对你有用。 Bash 是一种了不起的语言,它可以做它应该做的事情——管理 Linux 机器所需的任何事情。很难找到它做得不好的东西。值得花时间和它交朋友:)
【解决方案2】:

试试这个代码。

#!/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

【讨论】:

  • 失败:7-2: syntax error in expression (error token is "7-2")7+1: syntax error in expression (error token is "7+1")sed: -e expression #1, char 1: unknown command: ',' 另外,在修改系统配置文件时,始终保留原始文件。以防万一您的脚本不完全按照您的想法执行。
  • 这适用于Linux系统和Bash shell,你是在同一个平台还是在任何其他操作系统上执行?
  • 在 Archlinux 和 openSuSE 上测试过。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-11
  • 2017-11-21
  • 1970-01-01
  • 2015-03-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多