Condition checking issue while if

use single = instead of double(==)..

---------- Post updated at 08:56 AM ---------- Previous update was at 08:47 AM ----------

just try the below thing.. i am not sure, but just try once.

if test "$end_ct" = "END CAT" ; then
        echo "$end_ct"
else
        echo "value not found"
fi

---------- Post updated at 09:00 AM ---------- Previous update was at 08:56 AM ----------

are you using csh?? if its csh try with this syntax

if    ( Expression1 ) then 
    block A1
  else  if  ( Expression2 )
    block A2
  else  if  ( Expression3 )
    block A3
    ...
  else  if  ( Expression i-1 )
    block Ai-1
  else 
    block Ai
  endif

syntax differs in bash and csh

this time i have used this

if [ "end_ct" = "END CAT" ] ; then

but instead of returning true condition, it is falling to the else part

if [ "end_ct" = "END CAT" ] ; then
echo $end_ct
else
echo "value not found"
fi

it should echo $end_ct but it is returning echo "value not found"

you forgot to add $ in front of end_ct in the expression of if condition.

print the value of $end_ct just before the if statement to check what value it is getting.
just add a line before if

echo "end_ct = {$end_ct}"
if [ "$end_ct" = "END CAT" ]; then
      echo $end_ct
else
      echo "value not found"
fi

and dont forget to use the curly brackets {}. it easy to figure out if any extra spaces are there in the end_ct value.

so the output should be

end_ct = {END CAT}
END_CAT

as suggested, here is the updated code:

echo "end_ct = {$end_ct}"
if [ "$end_ct" = "END CAT" ] ; then
echo $end_ct
else
echo "value not found"
fi

and here is the output coming now:

so now you can see why u r getting wrong output..

your end_ct contain the value inside the curly brackets {} i.e.

"TRIM(DESCR
----------
END CAT" 

so end_ct and END CAT values are different and the output is value not found.. so check out your sql query and the result its returning. right now ur sql query is returning "TRIM(DESCR
----------
END CAT" value and not "END CAT".

yeah, i have seen this. but it is obvious that it will return column name along with the value.

end_ct=`sqlplus -s $ORACLE_USER/$ORACLE_PASS@$ORACLE_SID   << EOF
                select trim(description) from bravo_statistics
                where trunc(time_stamp)=trunc(sysdate)-5
                and description='END CAT';

how to get rid off from this situation now, please help

---------- Post updated at 10:07 AM ---------- Previous update was at 10:01 AM ----------

i have used another variable to check the value of end_ct and comparing these 2 variables now. it is returning the exact value which i require

a=$end_ct
and further my condition is 
if [ "$end_ct" = "$a" ] && [ $emtr_cnt -eq 1 ] ; then
echo $end_ct
else
echo "value not found"
fi

but whille using && condition it is showing some other error:
bravo_main_refresh.sh: test: unknown operator ----------

i am sorry for this, but my logic is incorrect for the first statement too. because i need to compare value with END CAT. please suggest how to compare value of a variable returning from a select statement.

just send the output to a temporary file instead of storing in a variable using

sqlplus -s $ORACLE_USER/$ORACLE_PASS@$ORACLE_SID   << EOF > sql_output.txt
                select trim(description) from bravo_statistics
                where trunc(time_stamp)=trunc(sysdate)-5
                and description='END CAT';
EOF

end_ct=`sed -n '3p' sql_output.txt`

just check the content of the sql_output.txt file and see in which line the output of the sql query occurs i.e. the END CAT value comes in which line and accordingly use the line number in the sed command.. i assumed the actual output (excluding the column name and the ---------) appears in the 3rd line so i have used 3p in the sed command. change the 3p to appropriate line number after checking the value inside sql_output.txt.

1 Like

i have modified my code as suggested:

end_ct=`sqlplus -s $ORACLE_USER/$ORACLE_PASS@$ORACLE_SID   << EOF > sql_output.txt
                select trim(description) from bravo_statistics
                where trunc(time_stamp)=trunc(sysdate)-5
                and description='END CAT';
EOF`
echo $end_ct;
emtr_cnt=`sqlplus -s $ORACLE_USER/$ORACLE_PASS@$ORACLE_SID   << EOF >> sql_output.txt
                select count(*) from mrs.bill_cal
                where trunc(actual_run_date)=trunc(sysdate)-4;
EOF`
echo $emtr_cnt;
#echo "end_ct = {$end_ct}"
a=`sed -n '4p' sql_output.txt`
b=`sed -n '9p' sql_output.txt`
echo "$a"
echo "$b"
if [ "$a" = "END CAT" ] && [$b -eq 1 ] ; then
echo "value found"
else
echo "value not found"
fi

now i am getting my desired output, please check if any syntax error is there otherwise i am all set. thanks for your help

[$b -eq 1 ] should be [ $b -eq 1 ] .

---- edit: below statement is incorrect as Little pointed out. I somehow missed the >> -----
You are never reading the sql_output.txt file that is created by the first sqlplus statement and it gets overwritten by the second.

1 Like

corrected and getting the output as it is.

syntax is proper. but try to replace

end_ct=`sqlplus -s $ORACLE_USER/$ORACLE_PASS@$ORACLE_SID   << EOF > sql_output.txt
                select trim(description) from bravo_statistics
                where trunc(time_stamp)=trunc(sysdate)-5
                and description='END CAT';
EOF`

with just

sqlplus -s $ORACLE_USER/$ORACLE_PASS@$ORACLE_SID   << EOF > sql_output.txt
                select trim(description) from bravo_statistics
                where trunc(time_stamp)=trunc(sysdate)-5
                and description='END CAT';
EOF

and similarly

emtr_cnt=`sqlplus -s $ORACLE_USER/$ORACLE_PASS@$ORACLE_SID   << EOF >> sql_output.txt
                select count(*) from mrs.bill_cal
                where trunc(actual_run_date)=trunc(sysdate)-4;
EOF`

with just

sqlplus -s $ORACLE_USER/$ORACLE_PASS@$ORACLE_SID   << EOF >> sql_output.txt
                select count(*) from mrs.bill_cal
                where trunc(actual_run_date)=trunc(sysdate)-4;
EOF

and u can replace a and b with end_ct and emtr_cnt respectively if u want. and remove the

echo $end_ct and 
echo $emtr_cnt

and put it after the two sed commands to c the values of these variables to be sure.

otherwise everything is fine.

---------- Post updated at 11:07 AM ---------- Previous update was at 11:02 AM ----------

its not getting overwritten but the second sql output is appended to the sql_output.txt file. so the sql_output.txt is having the output of both the sql queries.

> overwrites to the existing file
>> append to the file
2 Likes