在 BASH 中将变量的值括在单引号中

在 BASH 中将变量的值括在单引号中

我正在尝试调整一些Perl脚本以BashNagios XI.

问题是 Nagios 期望变量的值到达单引号之间,但我无法正确转义变量。

例如,在我声明变量和/或常量的顶部区域,我有:

status='OK'

当我将其替换为返回到 Nagios 的值时,我将其放置在这样的位置并且它可以正常工作:

output="[$status]"
output_msg="Voting disks status check succeeded"

打印时返回[OK] Voting disks status check succeeded.

但是,我还有根据执行的命令构建的其他变量,如下所示:

command=`/oracle/app/grid/19300/bin/crsctl query css votedisk | grep asm`
output1=`echo $command | cut -d " " -f4`
output2=`echo $command | cut -d " " -f5`

如果我在本地运行此脚本,结果如​​预期:

[OK] Voting disks status check succeeded - (/dev/mapper/asm_ocr1) [OCR].

但是当我从 Nagios 服务器启动它时,$output1 和 $output2 变量始终为空:

[OK] Voting disks status check succeeded -

我知道这是因为单引号,因为当我尝试用文字文本声明第三个变量时,它会正确显示:

output3='Test'
output="[$status] $output_msg - $output3

这在 Nagios 服务器上绘制:

[OK] Voting disks status check succeeded - Test

$output1知道如何将包含在单引号和单引号中的文本$output2以便 Nagios 解释它吗?

编辑

为了证实变量的内容必须用单引号引起来,我做了以下测试:

#Add the path that $output1 should return
test='/dev/mapper/asm_ocr1'
#Paint in the output the variable $test
output="[$status] $output_msg - $test $output1 $output2"

########

#On the Nagios server, the $test variable send correctly:
[OK] Voting disks status check succeeded - /dev/mapper/asm_ocr1 ''

编辑2

我发现错误不是来自引号,而是来自Nagios与运行DB的节点的通信。

到目前为止,完整的脚本是这样的:

. /home/oracle/.profile_RAC
ORACLE_HOME=/oracle/app/grid/19300
ORACLE_BASE=/oracle/app/base
nagios_exit_codes=('UNKNOWN', 3, 'OK', 0, 'WARNING', 1, 'CRITICAL', 2)
status='OK'
ok=1
action=$1

case $action in
        "votedisk")
                #command=`/oracle/app/grid/19300/bin/crsctl query css votedisk | grep asm`
                #command=$(/oracle/app/grid/19300/bin/crsctl query css votedisk)
                command=`/oracle/app/grid/19300/bin/crsctl query css votedisk`

                case $comando in
                        *"failed"*|*"OFFLINE"*|*"PROC"*)
                                status='CRITICAL'
                                output_msg="Voting disk status check failed!"
                        ;;

                        * )
                                output_msg="Voting disks status check succeeded"
                        ;;
                esac

                output="[$status] $output_msg - $command"

        ;;

        "clusterstatus")
                comando=`/oracle/app/grid/19300/bin/crsctl query crs releaseversion`
                output_msg="All clusterware services are up (clusterware version: $comando)"
                output="$output_msg"

        ;;
esac


echo -e $output
exit 0

在本地运行这个脚本,结果是这样的:

[root@bbddmachine plugins]# sh ./script_prueba.sh votedisk
[OK] Voting disks status check succeeded - ## STATE File Universal Id File Name Disk group -- ----- ----------------- --------- --------- 1. ONLINE 8dfc2a9528244f95bf87bb394e793995 (/dev/mapper/asm_ocr1) [OCR] Located 1 voting disk(s).

但在Nagios机器上,结果是错误的:

[nagios@ng1esp libexec]$ ./check_nrpe -2 -H 172.47.62.12 -t 60 -c check_crs_votedisk
[OK] Voting disks status check succeeded - Unable to communicate with the Cluster Synchronization Services daemon.

但是,如果我在名为“clusterstatus”的脚本中启动另一个选项,一切都会正常工作:

[nagios@ng1esp libexec]$ ./check_nrpe -2 -H 172.47.62.12 -t 60 -c check_crs_clusterstatus
All clusterware services are up (clusterware version: Oracle High Availability Services release version on the local node is [19.0.0.0.0])

相关内容