Problem evaluating condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem evaluating condition
# 1  
Old 01-26-2018
Problem evaluating condition

First, given this bit of code (line numbers included for reference:
Code:
 59 get_all_db () {
 60 #echo getting all db
 61 dblist=`egrep -i "product/12" /etc/oratab |grep -v "listener"|\
 62       awk -F\: '{print $1}'|sort`
 63 echo list is $dblist
 64 echo
 65 echo
 66 echo "INSTANCE_NAME    HTTPS Port  HTTP Port"
 67 echo "---------------- ---------- ----------"
 68 for ORACLE_SID in $dblist
 69 do
 70 echo checking for pmon_$ORACLE_SID
 71 ps -ef|grep pmon_$ORACLE_SID|grep -v grep
 72 echo ps command complete
 73   if ps -ef|grep pmon_$ORACLE_SID|grep -v grep
 74   then
 75     echo calling sqlplus
 76     . oraenv
 77     sqlplus -s /nolog <<EOF
 78     set echo off feedback off verify off head off
 79     conn / as sysdba
 80     select instance_name,
 81            DBMS_XDB_CONFIG.gethttpsport "HTTPS Port",
 82            DBMS_XDB_CONFIG.gethttpport "HTTP Port"
 83     FROM v\$instance;
 84 EOF
 85   fi
 86 done
 87 }

Producing this output:
Code:
  1 list is db12 fs91upg fs92dev fs92upg hr92dev hr92tst hr92upg
  2
  3
  4 INSTANCE_NAME    HTTPS Port  HTTP Port
  5 ---------------- ---------- ----------
  6 checking for pmon_db12
  7 ps command complete
  8 checking for pmon_fs91upg
  9 ps command complete
 10 checking for pmon_fs92dev
 11 ps command complete
 12 checking for pmon_fs92upg
 13 ps command complete
 14 checking for pmon_hr92dev
 15 ps command complete
 16 checking for pmon_hr92tst
 17 ps command complete
 18 checking for pmon_hr92upg
 19 ps command complete

What I don't understand is why the IF continues to evaluate FALSE. The first iteration of the DO loop, where the value of $ORACLE_SID is 'db12' should evaluate false, but the rest are true. To help debug I added lines 70 and 71 to show the current value of ORACLE_SID and the result of the same command that produces the result to be evaluated by the IF. You can see from the output that it appears to always return null (false) but if I execute the very same command manually, it returns the expected result:

Code:
  1 oracle:fs91upg$ echo $ORACLE_SID
  2 fs91upg
  3
  4 oracle:fs91upg$ ps -ef|grep pmon_$ORACLE_SID|grep -v grep
  5 oracle    2842     1  0 Jan22 ?        00:00:34 ora_pmon_fs91upg
  6
  7 2018-01-26 08:59:44
  8 oracle:fs91upg$


Last edited by edstevens; 01-26-2018 at 11:49 AM.. Reason: Adding reference line numbers to block of code
# 2  
Old 01-26-2018
I may say load of nonsense as just got home not too well and very tired so forgive me...
What I see is what you say give what you expected for an output is a command line:
Code:
 ps -ef|grep pmon_$ORACLE_SID|grep -v grep

What is it made of? I see 3 processes, and yes nothing wrong there so you get what is expected but
looking at your if, what do you think is the condition?


So what about replacing your if by :
Code:
if  [ -n  "$(ps -ef|grep pmon_$ORACLE_SID|grep -v grep)" ]

or the correct variant ( sorry can't test, and in bed now...)

Addendum:
That is what I would do in ksh...

Last edited by vbe; 01-26-2018 at 01:42 PM.. Reason: Addendum and correctd the if
# 3  
Old 01-26-2018
First, let me point out that I've edited my opening post, to add reference line numbers to all 'code' blocks. Herein I use the term 'code block' to refer to any section of my posts that are set off as 'code', regardless of whether it is actual script code, or other type of output taken from the shell session.

In the posted script, line 71 (the 'ps' command) should echo to stdout exactly what is being tested with the IF at line 73. The expected output for everything except the first iteration is shown in the last 'code' block in my opening post. For example, on the second iteration of the DO loop, the value of ORACLE_SID is 'fs91upg'. And we see this in the second code block, at line 8, as echoed from line 70 of the script. But we do not see any output from script line 71. That same command - line 71 of the script - produces the output shown at line 5 of the third 'code' block.

Of course, the fact that script line 71 never produces any output explains why the IF on line 73 is evaluating FALSE. So the question is not really about the IF, per se, but why that 'ps' command (both at script lines 71 and 73 is returning nulls when the very same command, with the very same values, returns expected data when issued from the command prompt.
# 4  
Old 01-26-2018
I'm afraid that using the result of a "command substitution" for an if condition is not too good an idea, as the shell it will try to run the result line as a command which will most probably fail.
What makes me somewhat hesitant is that
Code:
ps -ef|grep pmon_$ORACLE_SID|grep -v grep

doesn't seem to produce a result as can be seen in the output in lines 8 and 9. grep not producing an output line - and be it through the -v option - will yield the exit code 1 which is interpreted as FALSE.
Please show the result of the command
Code:
ps -ef|grep pmon_$ORACLE_SID

in your function. Is it possible the $ORACLE_SID has non-printing control chars in it?
# 5  
Old 01-26-2018
I woke up and corrected my if the way I would do that with ksh...
# 6  
Old 01-26-2018
I agree with RudiC that there doesn't appear to be anything obvious. Please add the following line of code between lines 86 and 87 in your get_all_db() function:
Code:
set +xv

then add the line:
Code:
set -xv

between lines 59 and 60 in that same function, then run your script again, and show us the output produced.

You could also replace:
Code:
 ps -ef|grep pmon_$ORACLE_SID|grep -v grep

in both places with:
Code:
 ps -ef|grep '[p]'mon_$ORACLE_SID

but this should only make the code run faster; not affect the output produced.
# 7  
Old 01-26-2018
Rudic -
To show the "raw" value of $ORACLE_SID at a command line:

Code:
oracle:hr92upg$ echo $ORACLE_SID
hr92upg

And to show no hidden characters, I'll append an 'x' on each end:

Code:
oracle:hr92upg$ echo x${ORACLE_SID}x
xhr92upgx

And, as requested, the ps and grep, without the -v to filter out the grep itself

Code:
oracle:hr92upg$ ps -ef|grep pmon_$ORACLE_SID
oracle    1237     1  0 10:37 ?        00:00:01 ora_pmon_hr92upg
oracle   31557 28288  0 12:59 pts/0    00:00:00 grep pmon_hr92upg

And in the interest of full disclosure, all possible 'pmon' processes:

Code:
oracle:hr92upg$ ps -ef|grep pmon
oracle    1237     1  0 10:37 ?        00:00:01 ora_pmon_hr92upg
oracle    1631     1  0 Jan22 ?        00:00:46 ora_pmon_fs91dmo
oracle    2842     1  0 Jan22 ?        00:00:35 ora_pmon_fs91upg
oracle    4317     1  0 Jan22 ?        00:00:49 ora_pmon_fs91dev
oracle    9786     1  0 Jan04 ?        00:03:03 ora_pmon_fs92upg
oracle   10156     1  0 Jan04 ?        00:03:02 ora_pmon_hr92dev
oracle   21794     1  0 Jan05 ?        00:02:58 ora_pmon_fs92dev
oracle   23261     1  0 Jan25 ?        00:00:11 ora_pmon_hr92tst
oracle   31578 28288  0 12:59 pts/0    00:00:00 grep pmon

Compare the above with the list of possible values for ORACLE_SID generated at line 61 of my script, and displayed at line 63
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Condition problem

Hi All, Seeking for your assistance on how to condition it correctly. cat file1.txt 290,1663,43,888,0,0.00,86.91,0.00,26.98,0.00 290,1663,52,0,0,0.00,0.00,0.00,0.00,0.00 290,1663,52,888,0,0.00,34.60,0.00,9.00,0.00 1st scenario: if the fourth column contains 888s and 0s it is by... (16 Replies)
Discussion started by: znesotomayor
16 Replies

2. Shell Programming and Scripting

If condition problem

Hi All, I am using below if condition to check whether null is passed as a parameter to the program if or ; then echo "ABC">>$FILE else echo "CDF">>$FILE fi However it is saying me null=null command not found . Please help me with this (9 Replies)
Discussion started by: Hypesslearner
9 Replies

3. Shell Programming and Scripting

Problem in if condition

Hi, below is the script in ksh and i am having issues with if condition. It takes in one argument as input and executes the shell script. The problem is in if condition in shell script. If input is given as 1 it works out well. But if input is given as 2 or something else the script is failing to... (1 Reply)
Discussion started by: abhi_123
1 Replies

4. Shell Programming and Scripting

problem with if condition

Hi, I'm writing a bash script and i have a condition that goes if then break fi but, when i go to run it, i come across this line 10: ' where line 10 is the if I don't know what's going on :( (2 Replies)
Discussion started by: channyboy
2 Replies

5. Shell Programming and Scripting

If condition problem

Hi, I need to use if condition for search a file pattern on a particular location. cd $file_Path if || then do this else do that fi Can someone help me with the if part, how i can put those conditions? make sure format should be *.file* and *.file file is a keyword which i... (5 Replies)
Discussion started by: amit.mathur08
5 Replies

6. Shell Programming and Scripting

if condition not evaluating as expected

Hi all, Thanks in advance for your time. I have a data file like this: 1 7.465753425 2 8.980821918 1 1.717808219 1 6.550684932 0 5.432876712 I wish to write a bash script to check both columns and output a 1 if col1==1 AND col2<3. Otherwise I want to output a 0. In the above... (5 Replies)
Discussion started by: jem8271
5 Replies

7. Shell Programming and Scripting

Problem in using AND OR condition together

a=rhino b=crocodil c=testsc if && "$c" = testsc ] then echo "Test #5 succeeds." else echo "Test #5 fails." fi i need to test or condition before check the output with AND condition. ur help is much appreciated... (11 Replies)
Discussion started by: gokulraj23
11 Replies

8. Shell Programming and Scripting

problem in if then else condition

Hi , I am trying the following simple script . But it is always giving 1 output. Dont know why #!/bin/sh find . -name "a.log" if ; then echo "1" else echo "0" fi Kindly advice. it is giving 1 output even when the a.log file is not there (26 Replies)
Discussion started by: himvat
26 Replies

9. Shell Programming and Scripting

problem in if condition

hi, actully i need the belp for the below. host_list=" Host1 host2 host3 host4 " n=`hostname` i need to put the condition like the below if n is among the host mention in the host_list if then #some stugg else # some other stuff fi (1 Reply)
Discussion started by: mail2sant
1 Replies

10. Shell Programming and Scripting

problem with if condition

hi, :) pls consider the following if statement if //g') ] then ........ else ....... when i execute the script i am getting the following error '(' unexpected I am not able to find the mistake. could anybody tell where i did mistake. cheers RRK (13 Replies)
Discussion started by: ravi raj kumar
13 Replies
Login or Register to Ask a Question