bash scripting with many ifs not working

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
psteam
Posts: 32
Joined: Fri Dec 02, 2011 12:03 pm
Location: Montreal, Quebec

Re: bash scripting with many ifs not working

Post by psteam »

here is the odd thing
i removed all the ifs in the bash..the script only contains is this now

Code: Select all

#!/bin/bash
#coded by ajdazeve
# version 3.2

script=""
instance=""
warn=0
crit=0
stactic=0
dis=0
user=""
pass=""


# Get options
while getopts :i:s:w:c:d:b:u: OPTION

do
        case $OPTION
        in
                i)
                                        instance=${OPTARG}
                                ;;
                s)
                                        script=${OPTARG}
                                ;;
                w)
                                        warn=${OPTARG}
                                ;;
                c)
                                        crit=${OPTARG}
                                ;;
                b)
                                        stactic=${OPTARG}
                                ;;
                d)
                                        dis=${OPTARG}
                                ;;								
                u)
                                        user=${OPTARG}
                                ;;										
        esac
done

# grab password from User


pass=$(awk -F "$user"= '{print $2}' .mdfpassword )



# extract output from query 

tmp_out_nospace=$(. Oracle.env;sqlplus -s "$user"/"$pass"@$instance @$script | tr -d '[[:space:]]'  )
echo $tmp_out_nospace

#tmp_out_nospace="$(echo -e "${tmp_out}" | tr -d '[[:space:]]')"

#output="result: "$tmp_out" "

# show all variables for debugging 
#echo $script
#echo $instance
#echo $warn
#echo $crit
#echo $stactic
#echo $dis
#echo $tmp_out
#echo $tmp_out_nospace
#echo $output
#echo $errcnt
#echo $user
#echo $pass
==================================
on the command line : i get this
[nagios@tbv1nagios8 oracle]$ ./scriptsqlplus.sh -s mon_nb_users.sql -i dora80 -u nagios -b 398
398
[nagios@tbv1nagios8 oracle]$

however one the nagios side i get what you see as screenshot.
(No output returned from plugin)

if i dont trim the spaces the output is something like this

[nagios@tbv1nagios8 oracle]$ . Oracle.env;sqlplus -s nagios/xxx@dora80 @mon_nb_users.sql

398

tbv1nagios8:nagios$
perhaps trimming the spaces is not the best idea.
You do not have the required permissions to view the files attached to this post.
Last edited by tmcdonald on Thu Feb 04, 2016 1:15 pm, edited 1 time in total.
Reason: Please use [code][/code] tags around long output
Mediagrif
Prod Support Team
tmcdonald
Posts: 9117
Joined: Mon Sep 23, 2013 8:40 am

Re: bash scripting with many ifs not working

Post by tmcdonald »

Run it from the CLI like so:

bash -x ./scriptsqlplus.sh -s mon_nb_users.sql -i dora80 -u nagios -b 398

Then post the output in code tags please.
Former Nagios employee
psteam
Posts: 32
Joined: Fri Dec 02, 2011 12:03 pm
Location: Montreal, Quebec

Re: bash scripting with many ifs not working

Post by psteam »

Code: Select all

tbv1nagios8:nagios$ bash -x ./scriptsqlplus.sh -s mon_nb_users.sql  -i dora80 -u nagios -b 398
+ script=
+ instance=
+ warn=-1
+ crit=-1
+ stactic=-1
+ dis=0
+ user=
+ pass=
+ getopts :i:s:w:c:d:b:u: OPTION
+ case $OPTION in
+ script=mon_nb_users.sql
+ getopts :i:s:w:c:d:b:u: OPTION
+ case $OPTION in
+ instance=dora80
+ getopts :i:s:w:c:d:b:u: OPTION
+ case $OPTION in
+ user=nagios
+ getopts :i:s:w:c:d:b:u: OPTION
+ case $OPTION in
+ stactic=398
+ getopts :i:s:w:c:d:b:u: OPTION
++ awk -F nagios= '{print $2}' .mdfpassword
+ pass=fdsk98342f
++ . Oracle.env
+++ PATH=/usr/X11R6/bin:/opt/admin/bin:/usr/local/bin:/bin:/usr/bin
+++ case "`uname`" in
++++ uname
+++ . /etc/profile
++++ '[' -x /usr/bin/id ']'
++++ '[' -z 559 ']'
+++++ id -un
++++ USER=nagios
++++ LOGNAME=nagios
++++ MAIL=/var/spool/mail/nagios
++++ '[' 559 = 0 ']'
++++ pathmunge /usr/local/sbin after
++++ case ":${PATH}:" in
++++ '[' after = after ']'
++++ PATH=/usr/X11R6/bin:/opt/admin/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin
++++ pathmunge /usr/sbin after
++++ case ":${PATH}:" in
++++ '[' after = after ']'
++++ PATH=/usr/X11R6/bin:/opt/admin/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin
++++ pathmunge /sbin after
++++ case ":${PATH}:" in
++++ '[' after = after ']'
++++ PATH=/usr/X11R6/bin:/opt/admin/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin
++++ pathmunge /opt/admin/bin
++++ case ":${PATH}:" in
+++++ /bin/hostname
++++ HOSTNAME=tbv1nagios8
++++ HISTSIZE=1000
++++ '[' ignoredups = ignorespace ']'
++++ export HISTCONTROL=ignoredups
++++ HISTCONTROL=ignoredups
++++ export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
++++ '[' 559 -gt 199 ']'
+++++ id -gn
+++++ id -un
++++ '[' nagios = nagios ']'
++++ umask 022
++++ for i in '/etc/profile.d/*.sh'
++++ '[' -r /etc/profile.d/colorls.sh ']'
++++ '[' hxB '!=' hxB ']'
++++ . /etc/profile.d/colorls.sh
++++ for i in '/etc/profile.d/*.sh'
++++ '[' -r /etc/profile.d/cvs.sh ']'
++++ '[' hxB '!=' hxB ']'
++++ . /etc/profile.d/cvs.sh
++++ for i in '/etc/profile.d/*.sh'
++++ '[' -r /etc/profile.d/glib2.sh ']'
++++ '[' hxB '!=' hxB ']'
++++ . /etc/profile.d/glib2.sh
++++ for i in '/etc/profile.d/*.sh'
++++ '[' -r /etc/profile.d/lang.sh ']'
++++ '[' hxB '!=' hxB ']'
++++ . /etc/profile.d/lang.sh
++++ for i in '/etc/profile.d/*.sh'
++++ '[' -r /etc/profile.d/less.sh ']'
++++ '[' hxB '!=' hxB ']'
++++ . /etc/profile.d/less.sh
++++ for i in '/etc/profile.d/*.sh'
++++ '[' -r /etc/profile.d/profile_mdf.sh ']'
++++ '[' hxB '!=' hxB ']'
++++ . /etc/profile.d/profile_mdf.sh
++++ for i in '/etc/profile.d/*.sh'
++++ '[' -r /etc/profile.d/qt.sh ']'
++++ '[' hxB '!=' hxB ']'
++++ . /etc/profile.d/qt.sh
++++ for i in '/etc/profile.d/*.sh'
++++ '[' -r /etc/profile.d/vim.sh ']'
++++ '[' hxB '!=' hxB ']'
++++ . /etc/profile.d/vim.sh
++++ for i in '/etc/profile.d/*.sh'
++++ '[' -r /etc/profile.d/which2.sh ']'
++++ '[' hxB '!=' hxB ']'
++++ . /etc/profile.d/which2.sh
++++ unset i
++++ unset -f pathmunge
++++ VAR_ACE=/opt/ace/data
++++ export VAR_ACE
+++ ORACLE_BASE=/opt/oracle
+++ ORACLE_HOME=/opt/oracle/product/11.2.0.4
+++ CLASSPATH=/opt/oracle/product/11.2.0.4/jlib
+++ LD_LIBRARY_PATH=/opt/oracle/product/11.2.0.4/lib:/opt/oracle/product/11.2.0.4/lib32:/usr/lib:/usr/lib64
+++ NLS_LANG=AMERICAN_AMERICA.UTF8
+++ ORA_NLS33=/opt/oracle/product/11.2.0.4/ocommon/nls/admin/data
+++ export DISPLAY ORACLE_BASE ORACLE_HOME CLASSPATH LD_LIBRARY_PATH NLS_LANG PATH
+++ PATH=/opt/oracle/product/11.2.0.4/bin:/usr/X11R6/bin:/opt/admin/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/nagios/bin:/home/nagios/util:/home/nagios/script/perl
+++ tty -s
+++ '[' 0 -eq 0 ']'
+++ stty erase '^H'
++++ hostname
+++ PS1='tbv1nagios8:nagios$ '
++ sqlplus -s nagios/fdsk98342f@dora80 @mon_nb_users.sql
+ tmp_out='
       398'
++ tr -d '[[:space:]]'
++ echo -e '
       398'
+ tmp_out_nospace=398
++ wc -l
++ grep -i ERROR
++ echo 398
+ errcnt=0
+ '[' 0 -ne 0 ']'
+ '[' 0 -eq 1 ']'
+ '[' '' == false ']'
+ ALERT=true
+ '[' 398 -ne -1 ']'
+ '[' 398 -ne 398 ']'
+ echo 'static OK: 398'
this is from the cli
Last edited by tmcdonald on Fri Feb 05, 2016 11:03 am, edited 1 time in total.
Reason: Please use [code][/code] tags around long output
Mediagrif
Prod Support Team
psteam
Posts: 32
Joined: Fri Dec 02, 2011 12:03 pm
Location: Montreal, Quebec

Re: bash scripting with many ifs not working

Post by psteam »

Hi everyone,
i found the solution i had to source out my env file like so
# extract output from query
source $path/Oracle.env;
tmp_out=$(sqlplus -s "$user"/"$pass"@$instance @"$path"/"$script" )
tmp_out_nospace="$(echo -e "${tmp_out}" | tr -d '[[:space:]]')"



i also had to create the variable path because all my calls of files required full path
thanks for the bash -x idea ...it helped figure out the issue.
Mediagrif
Prod Support Team
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: bash scripting with many ifs not working

Post by lmiltchev »

I'm glad your issue has been resolved! Is it safe to lock this thread?
Be sure to check out our Knowledgebase for helpful articles and solutions!
psteam
Posts: 32
Joined: Fri Dec 02, 2011 12:03 pm
Location: Montreal, Quebec

Re: bash scripting with many ifs not working

Post by psteam »

Yes thank you
Mediagrif
Prod Support Team
Locked