gpt4 book ai didi

linux - Bash:一个 IF 语句返回了意想不到的值,你能找到原因吗?

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

我写了一个脚本,可以在 CentOS 机器上自动下载和安装 Nagios NRPE。脚本的相关部分是:

read -r -p "How would you like to configure the NRPE daemon? [X]inetd / Standalone [D]aemon " DMN
if [ "DMN" = "x" -o "DMN" = "X" ];
then
DMNMODE="xinetd"
cat << EOF > $XINETDFILE
service nrpe
{
flags = REUSE
type = UNLISTED
port = $NRPEPORT
socket_type = stream
wait = no
user = $NGUSER
group = $NGGROUP
server = /usr/sbin/nrpe
server_args = -c $NRPECFG --inetd
log_on_failure += USERID
disable = no
only_from = 127.0.0.1 $NAGIOSSRV
}
EOF
/etc/init.d/xinetd restart
elif [ "DMN" = "d" -o "DMN" = "D" ];
then
chkconfig nrpe on ; $NRPESVC start
DMNMODE="daemon"

fi
function CheckInstMode {
if [ "DMNMODE" = "daemon" ];
then
$NRPESVC restart
else
$XINETDSVC restart
fi
}
read -r -p "Would you like to pull NRPE plugins from Nagios server? [y/n]" ANS1
if [ $ANS1 = "y" ];
then
if [ $ARCH = "64" -a $NGSARCH = "64" ] ;
then scp root@$NAGIOSSRV:$NGPLUGINS64/* $NGPLUGINS64/
chown -R $NGUSER.$NGGROUP $NGPLUGINS64
CheckInstMode
elif [ "$ARCH" = "64" -a "$NGSARCH" = "32" ] ;
then scp root@$NAGIOSSRV:$NGPLUGINS32/* $NGPLUGINS64/
chown -R $NGUSER.$NGGROUP $NGPLUGINS64
CheckInstMode
elif [ "$ARCH" = "32" -a "$NGSARCH" = "32" ] ;
then scp root@$NAGIOSSRV:$NGPLUGINS32/* $NGPLUGINS32/
chown -R $NGUSER.$NGGROUP $NGPLUGINS32
CheckInstMode
elif [ "$ARCH" = "32" -a "$NGSARCH" = "64" ] ;
then scp root@$NAGIOSSRV:$NGPLUGINS64/* $NGPLUGINS32/
chown -R $NGUSER.$NGGROUP $NGPLUGINS32
CheckInstMode
fi
fi

该函数背后的想法是检查选择了哪种安装模式并重新启动 /etc/init.d/xinetd 如果选择了 Xinetd 或重新启动 /etc/init.d/nrpe 如果选择了 NRPE。我在 Debug模式下运行脚本 (sh -x script),这是输出:

+ case $ARCH in
+ echo 'Configuring /etc/nagios/nrpe.cfg'
Configuring /etc/nagios/nrpe.cfg
+ cat
+ echo 'Adding nagios user to /etc/sudoers'
Adding nagios user to /etc/sudoers
+ echo 'Defaults:nagios !requiretty'
+ echo 'nagios ALL = NOPASSWD:/usr/lib64/nagios/plugins/*'
+ echo 'Setting ownership of nagios and nagios plugins folders to nagios user'
Setting ownership of nagios and nagios plugins folders to nagios user
+ chown -R nagios.nagios /etc/nagios
+ chown -R nagios.nagios /usr/lib64/nagios/plugins
+ read -r -p 'How would you like to configure the NRPE daemon? [X]inetd / Standalone [D]aemon ' DMN
How would you like to configure the NRPE daemon? [X]inetd / Standalone [D]aemon x
+ '[' DMN = x -o DMN = X ']'
+ '[' DMN = d -o DMN = D ']'
+ read -r -p 'Would you like to pull NRPE plugins from Nagios server? [y/n]' ANS1
Would you like to pull NRPE plugins from Nagios server? [y/n]y
+ '[' y = y ']'
+ '[' 64 = 64 -a 64 = 64 ']'
+ scp 'root@IP:/usr/lib64/nagios/plugins/*' /usr/lib64/nagios/plugins/
1 100% 71 0.1KB/s 00:00
Back-check_services.pl 100% 2485 2.4KB/s 00:00
cacticpu.sh 100% 189 0.2KB/s 00:00
check_apt 100% 99KB 99.4KB/s 00:00
check_breeze 100% 2253 2.2KB/s 00:00
check_by_ssh 100% 41KB 40.6KB/s 00:00
check_clamd 100% 46KB 46.5KB/s 00:00
+ chown -R nagios.nagios /usr/lib64/nagios/plugins
+ CheckInstMode
+ '[' DMNMODE = daemon ']'
+ /etc/init.d/nrpe restart
Shutting down nrpe: [ OK ]
Starting nrpe: [ OK ]
+ stat --format=%Y /etc/passwd
+ chmod +x /usr/local/share/applications/file
+ read -r -p 'Would you like to test NRPE? [y/n]' ANS2
Would you like to test NRPE? [y/n]

如您所见,预期为 xinetdDMNMODE 变量是 daemon,即使我对 xinetd 问题的输入或守护进程是“x”。这会导致脚本重新启动 nrpe,即使选择了 xinetd,您能告诉我我做错了什么吗?

编辑 #1:我仍然从变量 $DMNMODE 得到了意外的结果,这是调试运行:

How would you like to configure the NRPE daemon? [X]inetd / Standalone [D]aemon x
+ [[ x = [Xx] ]]
+ DMNMODE=xinetd
+ cat
+ /etc/init.d/xinetd restart
Stopping xinetd: [ OK ]
Starting xinetd: [ OK ]
+ read -r -p 'Would you like to pull NRPE plugins from Nagios server? [y/n]' ANS1
Would you like to pull NRPE plugins from Nagios server? [y/n]y
+ '[' y = y ']'
+ '[' 64 = 64 -a 64 = 64 ']'
+ scp 'root@IP:/usr/lib64/nagios/plugins/*' /usr/lib64/nagios/plugins/
1 100% 71 0.1KB/s 00:00
Back-check_services.pl 100% 2485 2.4KB/s 00:00
cacticpu.sh 100% 189 0.2KB/s 00:00
check_apt 100% 99KB 99.4KB/s 00:00
negate 100% 30KB 29.7KB/s 00:00
plugins.pm 100% 1939 1.9KB/s 00:00
+ chown -R nagios.nagios /usr/lib64/nagios/plugins
+ CheckInstMode
+ '[' xinetd = daemon ']'
+ /etc/init.d/nrpe restart
Shutting down nrpe: [ OK ]
Starting nrpe: [ OK ]
+ stat --format=%Y /etc/passwd
+ chmod +x /usr/local/share/applications/file
+ read -r -p 'Would you like to test NRPE? [y/n]' ANS2

我的问题是... 为什么函数返回错误值?如果向上滚动,您可以看到我在问题中输入了 X,这意味着 DMNMODE 变量设置为 xinetd 而不是 nrpe...你能试着找出问题所在吗?

非常感谢您的帮助!板材

最佳答案

$DMN 变量引用缺少 $ 符号。

if [ "$DMN" = "x" -o "$DMN" = "X" ];

关于linux - Bash:一个 IF 语句返回了意想不到的值,你能找到原因吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26452337/

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