Link to home
Start Free TrialLog in
Avatar of celtician
celticianFlag for American Samoa

asked on

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?
Avatar of Tintin
Tintin

if [[ $VAR == SI || $VAR == NO ]]
Avatar of celtician

ASKER

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 ???
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
"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 ] ...
What do you see from
/home/userName/scripts/script2.ksh | od -c
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
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.
Will that variable just contain "SI" or "NO" or is there more text in there?
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} ]]
This question has been solved in some other site.... using case instead of if.
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.
Would you be so kind to post how you solved this? Thanks.
ASKER CERTIFIED SOLUTION
Avatar of celtician
celtician
Flag of American Samoa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial