Condition checking issue while if

hi,

i am using a simple condition

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

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

then i am getting following error message :
bravo_main_refresh.sh: test: ] missing

i exactly don't know what is missing here. please present you views on this.

Spaces are important in this syntax. Please change the if condition as below.

if [ $end_ct = "END CAT" ]; 

after changing the condition to

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

it is now throwing below error:
bravo_main_refresh.sh: test: unknown operator ----------

Since the expansion of $end_ct also contains whitespace characters, you need to quote it. Try:

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

thanks, now atleast i am not getting white space error, but it is not actually meeting the given value.

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

i have echo'ed the value from the select statement and it is "END CAT" only but while checking condition it is falling to the else condition rather . please suggest.

Please show us the exact output you get from:

set -xv
if [ "$end_ct" = "END CAT" ]; then
echo "X${end_ct}X"
else
echo "value not found"
fi
set +xv

well my first condition is working now, but i have to add 1 more condition into this if statement.

if [ "$end_ct" -eq "END CAT" ] && [ "$emtr_cnt" = 1 ]; then
  echo $end_ct
else
  echo "value not found"
fi

but same thing i can see here, again it is going to else condition.

For comparing numeric values you may use the -eq operator and there no need to enclose the variable holding numeral in quotes.

if [ "$end_ct" = "END CAT" ] && [ $emtr_cnt -eq 1 ]; then
echo $end_ct
else
echo "value not found"
fi

have made changes into the below code as suggest:

if [ "$end_ct" -eq "END CAT" ] && [ $emtr_cnt -eq 1 ]; then

but now it is giving error "bravo_main_refresh.sh: test: unknown operator ----------"

For the first condition to match string use "=" instead of "-eq" like I posted above.

but when i am using = in the first condition then it is giving error "bravo_main_refresh.sh: test: unknown operator ----------"

now my condition is like the following:

if [ "$end_ct" -eq "END CAT" ] || [ $emtr_cnt -eq 1 ]; then

i have changed && to ||, and its working properly now.

It may be working, but it is not working properly.

When comparing strings, use the = operator; when comparing numbers, use the -eq operator.

If you want the then code to run only when both conditions are TRUE, use the && operator to separate the test commands. If you want the then code to run when either or both conditions are TRUE, use the || operator to separate the test commands.

So, you need:

if [ "$end_ct" = "END CAT" ] || [ $emtr_cnt -eq 1 ]; then
        or
if [ "$end_ct" = "END CAT" ] && [ $emtr_cnt -eq 1 ]; then

You said you were adding a condition, but you never said whether you require both conditions to evaluate to TRUE or if you only require one of the conditions to evaluate to TRUE.

1 Like

i actually don't know the difference between && and ||. can you please elaborate.

test1 && test2 evaluates to TRUE if test1 evaluates to TRUE and test2 evaluates to TRUE. (Note that test2 won't be evaluated if test1 evaluates to FALSE.)

test1 || test2 evaluates to TRUE if test1 evaluates to TRUE or test2 evaluates to TRUE, or both evaluate to TRUE. (Note that test2 won't be evaluated if test1 evaluates to TRUE.)

1 Like

above reply is really helpful, but i am facing error when i used = operator in the first condition:

if [ "$end_ct" = "END CAT" ] || [ $emtr_cnt -eq 1 ]; then

and the error which i am facing is :

bravo_main_refresh.sh: test: unknown operator ----------

So we know that $end_cnt does not expand to END CAT and we know that $emtr_cnt does not expand to a string of decimal digits.

Several posts back, I asked you to turn on tracing and show us the output. You never did that. Please help yourself and us by turning tracing on early in your script with:

set -xv

show us your complete updated script, and show us the output from the trace if it isn't obvious what is going wrong when you look at the trace output.

If you are not sure do an example longhand to get your head around it!

An example:-

#!/bin/bash --posix
# if_then.sh
end_ct="END DOG"
emtr_cnt=0
# If first is correct _OR_ second is correct...
if [ "$end_ct" == "END CAT" ] || [ $emtr_cnt -eq 1 ]
then
	echo "Woohoo at least one works..."
else
	echo "Neither are correct!!!"
fi
# The first result will be "Neither are correct!!!"
# Now change one or both of the two variables to a correct value and rerun!

Change one or both of the variables to the correct value and rerun...

Last login: Fri Jul 19 12:48:00 on ttys000
AMIGA:barrywalker~> ./if_then.sh
Neither are correct!!!
AMIGA:barrywalker~> 

here is my updated string:

#!/bin/csh


# ***  Lovely Sethi 08/2013  ***
#  This process executes the BRAVO_MAIN_REFRESH.sql
#
#  This file should be scheduled in CRONTAB to run each Big run.

#  /uv1402/u207/home/bravodba/bestdbscript/shscriptfiles/RUNDATE.sh

end_ct=`sqlplus -s $ORACLE_USER/$ORACLE_PASS@$ORACLE_SID   << EOF
                select description from bravo_statistics
                where trunc(time_stamp)=trunc(sysdate)-4
                and description='END CAT';
EOF`
echo $end_ct;
emtr_cnt=`sqlplus -s $ORACLE_USER/$ORACLE_PASS@$ORACLE_SID   << EOF
                select count(*) from mrs.bill_cal
                where trunc(actual_run_date)=trunc(sysdate)-3;
EOF`
echo $emtr_cnt;
if [ "$end_ct" -eq "END CAT" ] && [ $emtr_cnt -eq 1 ]; then
echo $end_ct
else
echo "value not found"
fi

#sqlplus $ORACLE_USER/$ORACLE_PASS@$ORACLE_SID @/uv1402/u207/home/bravodba/bestdbscript/sqlscriptfiles/BRAVO_MAIN_REFRESH.sql
#/uv1402/u207/home/bravodba/bestdbscript/shscriptfiles/sendmailanalysisrefresh.sh

value of both the variables are true, so it should echo $end_ct, but instead it is first giving an error:

please suggest

$end_ct contains a numeric value or a string value??

if it contains string value then use

if [ "$end_ct" = "END CAT" ] && [ $emtr_cnt -eq 1 ]; then
      echo $end_ct
else
      echo "value not found"
fi

-eq is normally used for comparing numeric values.

i have already done this, but it is giving some error like :

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

and the error is :

don't know why