Comparing Unix String Variable

IM trying to compare a unix variable (a string within it) to some string and it isn't working.

The variable will ONLY contain either "SI" (yes in spanish) or "NO", and im comparing it this way:

if [ $VAR == *"NO"* ]; then 
	echo "Resource Not loaded </h2>" >> $MAIL_FILE
else
	echo "Resource Loaded sucessfully" >> $MAIL_FILE
fi;

Open in new window



VAR is created a few lines before:

VAR=$(/home/userName/scripts/script2.ksh | egrep 'SI|NO')

Open in new window


I tried also with just one "=" operator and still the same...

What im i doing wrong?
LVL 1
celticianAsked:
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.

TintinCommented:
if [[ $VAR == SI || $VAR == NO ]]
0
celticianAuthor Commented:
I just tried

 if [[ $VAR == NO ]];

and its not working, it keeps going to the "SI", i fear there might be some other extra character being sent from the script2.ksh ???
0
TintinCommented:
Either change

VAR=$(/home/userName/scripts/script2.ksh | egrep 'SI|NO')

to

VAR=$(/home/userName/scripts/script2.ksh | egrep -o 'SI|NO')

or do

if echo "$VAR" | egrep -q 'SI|NO'
then
      echo NO
else
      echo YES
fi
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

celticianAuthor Commented:
"egrep -o" doesn't seem to be working, i get the next error:

"egrep: illegal option -- o"
usage: egrep [ -bchilnsv ] [ -e exp ] [ -f file ] [ strings ] [ file ] ...
0
ozoCommented:
What do you see from
/home/userName/scripts/script2.ksh | od -c
0
Carlos IjalbaSenior SysadminCommented:
Try with:

if [ "$VAR" -eq "NO" ]
  then
      echo "Resource Not loaded </h2>" >> $MAIL_FILE
else
      echo "Resource Loaded sucessfully" >> $MAIL_FILE
fi

or if you prefer:

if [ "$VAR" == "NO" ]
  then
      echo "Resource Not loaded </h2>" >> $MAIL_FILE
else
      echo "Resource Loaded sucessfully" >> $MAIL_FILE
fi
0
celticianAuthor Commented:
ozo:

When i execute that command, i get:

/home/userName/scripts/script2.ksh | od -c

0000000 050141 071563 073557 071144 035040 006412 047117 006412
0000020 041557 067156 062543 072151 067556 020164 067440 072145
0000040 071557 066062 031061 020143 066157 071545 062056 006415
0000060 005000
0000061
od: cannot open c: No such file or directory


Carlos,

When executing the first option ( [ "$VAR" -eq "NO" ])

i get an error:

script.ksh[17]: NO^M: bad number


And with the second option it isn't working, it keeps going thru the else ((for the yes/si), when the variable obviously contains "NO" and is being printed in the mail subject.

I dont know if this sheds some light, i really dont know what else to check.
0
Gerwin Jansen, EE MVETopic Advisor Commented:
Will that variable just contain "SI" or "NO" or is there more text in there?
0
ozoCommented:
Since $VAR apparently contains ^M, besides the "SI" or "NO", it may be better to use ${VAR//[^A-Z]}
or you might test with
if [[ ${VAR/SI} !=  ${VAR/NO} ]]
0
celticianAuthor Commented:
This question has been solved in some other site.... using case instead of if.
0
ozoCommented:
There are many ways it can be solved.  Which solution to use is up to you.
We can only give you advice on which to use based on criteria that you give us about how you would form preferences between solutions.
0
Gerwin Jansen, EE MVETopic Advisor Commented:
Would you be so kind to post how you solved this? Thanks.
0
celticianAuthor Commented:
yes, it was solved instead of using if using case, this has been the solution in the code:

Instead of using  
if [[ $VAR == SI || $VAR == NO ]]

Open in new window


which somehow was not working, i have set:

case $var in
  *SI*) echo "Resource found" >> $MAIL_FILE ;;
  *NO*) echo "Resource not found" >> $MAIL_FILE ;;
  *) echo "Unable to test" >> $MAIL_FILE  ;;
esac

Open in new window


and now works as expected :)
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
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
Unix OS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.