ksh script and comparing strings

In my script I have a variable db_role which equals to "PHYSICAL STANDBY"

when I run the code below

 if [ ${db_role} == 'PHYSICAL STANDBY' ]
 then


fi

I get an error: STANDBY: 0403-012 A test command parameter is not valid.

How can I fix it?
LVL 35
YZlatAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MikeOM_DBACommented:
Try:
if [ "${db_role}" = 'PHYSICAL STANDBY' ]
 then

fi

Open in new window

0
YZlatAuthor Commented:
now no error but it does not enter the if statemement.

I tried:

if [ "${db_role}" = 'PHYSICAL STANDBY' ]
 then
echo "worked"

else
echo $db_role
fi

and it outputs "PHYSICAL STANDBY"
0
simon3270Commented:
Is the variable definitely exactly "PHYSICAL STANDBY"?  No spaces at ether end?  Exactly one space in the middle?

Try:

    echo "<${db_role}>"

to check for spaces at either end, and if that doesn't show a difference, try

    echo "${db_role}" | od -c

to see which characters are present (there will be a "\n" at the end - that's the "echo" command
0
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

YZlatAuthor Commented:
it printed out
<
PHYSICAL STANDBY>

how can I trim the variable of spaces?
0
MikeOM_DBACommented:
It;s more like a CR or LF . . . try this:

db_role="echo $db_role|tr -d '\n'`"

Open in new window

0
ozoCommented:
if you know there will be a newline at the beginning of $db_role, you might also do
if [ "$db_role" = '  
PHYSICAL STANDBY' ]
then 
  echo "worked"
else
   echo "<$db_role>"
fi

Open in new window

or
if [ "${db_role##
}" = 'PHYSICAL STANDBY' ]
then 
  echo "worked"
else
   echo "<${db_role##    
> }>"
fi

Open in new window

0
YZlatAuthor Commented:
Mike, your last suggestion resulted in this:

<echo
PHYSICAL STANDBY|tr -d '
'>
0
ozoCommented:
I think Mike meant to say
db_role=`echo $db_role|tr -d '\n'`
rather than
db_role="echo $db_role|tr -d '\n'`"
0
simon3270Commented:
You don't need the "tr" here, as long as you don't put double quotes round the variable name:

    db_role=`echo $db_role`

or the more readable, if your shell supports it:

    db_role=$(echo $db_role)

will drop newlines, leading and trailing blanks, and reduce multiple blanks within the string to single blanks.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
YZlatAuthor Commented:
Perfect!
0
YZlatAuthor Commented:
Thanks all!
0
YZlatAuthor Commented:
simon3270, `echo $db_role` solution output is -role
0
simon3270Commented:
Did you type `echo $db-role` by mistake? (hyphen rather than underscore)
0
YZlatAuthor Commented:
LOL my bad, I did
0
YZlatAuthor Commented:
I requested the question to be re-opened so i could include simon's answer and ad dmore points.
0
YZlatAuthor Commented:
Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.