gpt4 book ai didi

linux - Bash 脚本,不包括值

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:35:45 25 4
gpt4 key购买 nike

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

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
}

当我运行脚本时,第一个文件中的 contact* 行 append 到第二个文件,结果是:

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* 在 contact* 行中。我想添加到我的脚本中以跳过第一个文件中带有 xi* 的值,而不是将它们 append 到第二个文件中。 :/

我知道我可以使用 sed ->

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

但我无法让它在我的脚本中工作,我不知道该把它放在哪里。 :/

最佳答案

我认为你的脚本的内容可以这样写:

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"

关于linux - Bash 脚本,不包括值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49307506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com