Bash脚本,不包括值

时间:2018-03-15 19:11:26

标签: linux bash scripting append skip

我有一个脚本可以复制第一个文件中以联系人*开头的行,并将其粘贴到每个定义服务中的第二个文件{文件的一部分。第一个文件如下所示:

define host {
    host_name                       10.80.12.53
    use                             xiwizard_passive_host
    address                         10.80.12.53
    check_command                   check-host-alive!!!!!!!!
    max_check_attempts              5
    check_interval                  5
    retry_interval                  1
    check_period                    xi_timeperiod_24x7
    contacts                        Marko Geršić,Mijo,nagiosadmin,Patrick,ximgersic
    contact_groups                  UNIX
    notification_interval           60
    notification_period             xi_timeperiod_24x7
    icon_image                      passiveobject.png
    statusmap_image                 passiveobject.png
    _xiwizard                       passiveobject
    register                        1
    }

第二个:

define service {
    host_name                       10.80.12.53
    service_description             Service Status - mysqld2
    use                             local-service
    check_command                   check_xi_service_status!mysqld!!!!!!!
    register                        1
    }

define service {
    host_name                       10.80.12.53
    service_description             Service Status - npcd
    use                             local-service
    check_command                   check_xi_service_status!npcd!!!!!!!
    register                        1
    }

当我运行脚本时,第一个文件中的联系人*行被附加到第二个文件,结果是:

define service {
    host_name                       10.80.12.53
    service_description             Service Status - mysqld2
    use                             local-service
    check_command                   check_xi_service_status!mysqld!!!!!!!
    contacts                        Marko Geršić,Mijo,nagiosadmin,Patrick,ximgersic
    contact_groups                  UNIX
    register                        1
    }

define service {
    host_name                       10.80.12.53
    service_description             Service Status - npcd
    use                             local-service
    check_command                   check_xi_service_status!npcd!!!!!!!
    contacts                        Marko Geršić,Mijo,nagiosadmin,Patrick,ximgersic
    contact_groups                  UNIX
    register                        1
    }

这是脚本:

#!/bin/bash
shopt -s extglob

if (( $# != 2 )); then
  echo Usage: nagios-contacts.sh host-file service-file >&2
  exit 1
 fi

declare -A CONFIG CONFIGS
while read KEY VALUE; do
  [[ $KEY == contact@(s|_groups) ]] && CONFIG[$KEY]="$VALUE"
    done <$1

while read LINE; do
  if [[ $LINE == *"define service {"* ]]; then
    for KEY in ${!CONFIG[*]}; do
    CONFIGS[$KEY]=0
    done
  elif [[ $LINE == *}* ]]; then
    for KEY in ${!CONFIG[*]}; do
      [[ ${CONFIGS[$KEY]} == 1 ]] && unset CONFIGS[$KEY]
    done
    for KEY in ${!CONFIGS[*]}; do
      echo $KEY ${CONFIG[$KEY]}
    done
    unset CONFIGS
    declare -A CONFIGS
  elif [[ $LINE == *contact@(s|_groups)* ]]; then
    read KEY VALUE <<<"$LINE"
    CONFIGS[$KEY]=1
    LINE="$LINE,${CONFIG[$KEY]}"
  fi
  echo "$LINE"
done <$2 | tee $2.new
mv $2.new $2
echo Saved output to $2.new

第二个文件需要如下所示:

define service {
    host_name                       10.80.12.53
    service_description             Service Status - mysqld2
    use                             local-service
    check_command                   check_xi_service_status!mysqld!!!!!!!
    contacts                        Marko Geršić,Mijo,nagiosadmin,Patrick
    contact_groups                  UNIX
    register                        1
    }

define service {
    host_name                       10.80.12.53
    service_description             Service Status - npcd
    use                             local-service
    check_command                   check_xi_service_status!npcd!!!!!!!
    contacts                        Marko Geršić,Mijo,nagiosadmin,Patrick
    contact_groups                  UNIX
    register                        1
    }

所以,没有联系*行中的xi *。我想添加到我的脚本中,从第一个文件中跳过xi *的值,而不是将它们附加到第二个文件中。 :/

我知道我可以用sed实现这一点 - &gt;

sed'/ contact * / s / xi [^] * // g'

但我不能让它在我的剧本中工作,我不知道在哪里放。 :/

1 个答案:

答案 0 :(得分:1)

我认为你的剧本可以这样写:

contacts=$( grep '^[[:blank:]]*contact.' "$1" | sed 's/,xi[[:alpha:]]*//' )
temp=$(mktemp)
awk -v c="$contacts" '$1 == "register" {print c} 1' "$2" > "$temp" && mv "$temp" "$2"