Comparing two strings (one may be a null)

Comparing two strings (one may be a null)

am 05.01.2008 20:08:05 von markhobley

I have some variable foo, and I was expecting to be able to compare it
to a literal string "bar", so I coded:

if [ $foo = bar ] ; then
echo 'Ok, they are the same.'
fi

If foo is null, I get an error "unexpected operator". So I can't compare
a string if it has a null value.

I now try:

if [ -n $foo ] ; then
if [ $foo = bar ] ; then
echo 'Ok, they are the same.'
fi
fi

I am still getting the error, presumably because the first (outermost)
if conditional statement falls through because $foo is null.

What should I be doing here to compare $foo to the literal string "bar"?

Mark.

--
Mark Hobley,
393 Quinton Road West,
Quinton, BIRMINGHAM.
B32 1QE.

Re: Comparing two strings (one may be a null)

am 05.01.2008 20:13:42 von Cyrus Kriticos

Mark Hobley wrote:
> I have some variable foo, and I was expecting to be able to compare it
> to a literal string "bar", so I coded:
>
> if [ $foo = bar ] ; then
> echo 'Ok, they are the same.'
> fi
>
> If foo is null, I get an error "unexpected operator". So I can't compare
> a string if it has a null value.
>
> I now try:
>
> if [ -n $foo ] ; then
> if [ $foo = bar ] ; then
> echo 'Ok, they are the same.'
> fi
> fi
>
> I am still getting the error, presumably because the first (outermost)
> if conditional statement falls through because $foo is null.
>
> What should I be doing here to compare $foo to the literal string "bar"?

if [ "$foo" = "bar" ] ; then
^ ^ ^ ^
--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.

Re: Comparing two strings (one may be a null)

am 05.01.2008 21:50:35 von cfajohnson

On 2008-01-05, Mark Hobley wrote:
>
> I have some variable foo, and I was expecting to be able to compare it
> to a literal string "bar", so I coded:
>
> if [ $foo = bar ] ; then
> echo 'Ok, they are the same.'
> fi
>
> If foo is null, I get an error "unexpected operator". So I can't compare
> a string if it has a null value.
>
> I now try:
>
> if [ -n $foo ] ; then
> if [ $foo = bar ] ; then
> echo 'Ok, they are the same.'
> fi
> fi
>
> I am still getting the error, presumably because the first (outermost)
> if conditional statement falls through because $foo is null.
>
> What should I be doing here to compare $foo to the literal string "bar"?

Quote your variables: "$foo" not $foo

--
Chris F.A. Johnson, author
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence

Re: Comparing two strings (one may be a null)

am 05.01.2008 23:08:50 von markhobley

Cyrus Kriticos wrote:

> if [ "$foo" = "bar" ] ; then

Ahh ok. It's easy when you know how.

Out of interest, I thought that if $foo was null, then it would evaluate
as null, and not get passed as a parameter to the underlying test
command. One of my Unix shell programming books actually states that
this is so. Anyhow, the doublequote fix worked, so the book is obviously
wrong.

--
Mark Hobley,
393 Quinton Road West,
Quinton, BIRMINGHAM.
B32 1QE.

Re: Comparing two strings (one may be a null)

am 05.01.2008 23:29:42 von Cyrus Kriticos

Mark Hobley wrote:
> Cyrus Kriticos wrote:
>
>> if [ "$foo" = "bar" ] ; then
>
> Ahh ok. It's easy when you know how.
>
> Out of interest, I thought that if $foo was null, then it would evaluate
> as null, and not get passed as a parameter to the underlying test
> command. One of my Unix shell programming books actually states that
> this is so. Anyhow, the doublequote fix worked, so the book is obviously
> wrong.

Add "set -x" in a extra line before your "if" and start your script to see
what your shell do:

--- cut here ---
#!/bin/bash

set -x
foo=bar

if [ "$foo" = "bar" ] ; then
echo 'Ok, they are the same.'
fi
--- cut here ---
Output:

+ foo=bar
+ '[' bar = bar ']'
+ echo 'Ok, they are the same.'
Ok, they are the same.

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.

Re: Comparing two strings (one may be a null)

am 06.01.2008 03:08:05 von markhobley

Cyrus Kriticos wrote:

> set -x

Blimey it takes me back to BBC Micro and Trash 80 days, when
you used to type ECHO ON to see your programs running.

Mark.

--
Mark Hobley,
393 Quinton Road West,
Quinton, BIRMINGHAM.
B32 1QE.

Re: Comparing two strings (one may be a null)

am 06.01.2008 06:12:47 von Barry Margolin

In article ,
markhobley@hotpop.donottypethisbit.com (Mark Hobley) wrote:

> Cyrus Kriticos wrote:
>
> > if [ "$foo" = "bar" ] ; then
>
> Ahh ok. It's easy when you know how.
>
> Out of interest, I thought that if $foo was null, then it would evaluate
> as null, and not get passed as a parameter to the underlying test
> command. One of my Unix shell programming books actually states that
> this is so. Anyhow, the doublequote fix worked, so the book is obviously
> wrong.

The book is correct. That's why you need the quotes: it solves that
problem.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***

Re: Comparing two strings (one may be a null)

am 06.01.2008 14:15:27 von Maxwell Lol

Cyrus Kriticos writes:

> if [ "$foo" = "bar" ] ; then

Another approach is to use

if [ X$foo = Xbar ] ; then

That's in case $foo has the value of "=", which would case the line to
be evaluated as

if [ = = bar ]

which can generate a syntax error.
But if foo has a space, that fails as well.
Which leads to combining the two:

if [ "X$foo" = Xbar ] ; then

Cheers....

Re: Comparing two strings (one may be a null)

am 06.01.2008 19:06:42 von kevin.hehl

On Jan 5, 12:08=A0pm, markhob...@hotpop.donottypethisbit.com (Mark
Hobley) wrote:
> I have some variable foo, and I was expecting to be able to compare it
> to a literal string "bar", so I coded:
>
> if [ $foo =3D bar ] ; then
> =A0 echo 'Ok, they are the same.'
> fi
>
> If foo is null, I get an error "unexpected operator". So I can't compare
> a string if it has a null value.
>
> I now try:
>
> if [ -n $foo ] ; then
> =A0 if [ $foo =3D bar ] ; then
> =A0 =A0 echo 'Ok, they are the same.'
> =A0 fi
> fi
>
> I am still getting the error, presumably because the first (outermost)
> if conditional statement falls through because $foo is null.
>
> What should I be doing here to compare $foo to the literal string "bar"?
>
> Mark.
>
> --
> Mark Hobley,
> 393 Quinton Road West,
> Quinton, BIRMINGHAM.
> B32 1QE.

you need to quote the values

if [ "$foo" =3D "bar" ] ;

that way, if $foo is null, the quotes make it a 'emtpy' string
comparison.

Re: Comparing two strings (one may be a null)

am 07.01.2008 07:07:32 von Stephane CHAZELAS

On 06 Jan 2008 08:15:27 -0500, Maxwell Lol wrote:
> Cyrus Kriticos writes:
>
>> if [ "$foo" = "bar" ] ; then
>
> Another approach is to use
>
> if [ X$foo = Xbar ] ; then
>
> That's in case $foo has the value of "=", which would case the line to
> be evaluated as
>
> if [ = = bar ]
>
> which can generate a syntax error.

Only in non-standard implementations of "[", though.

> But if foo has a space, that fails as well.
> Which leads to combining the two:
>
> if [ "X$foo" = Xbar ] ; then
[...]

Yes, leaving a variable unquoted has a very special meaning to
the shell, you should really think twice (and then twice again
after a pause) before doing so.

--
Stephane

Re: Comparing two strings (one may be a null)

am 08.01.2008 08:53:13 von Rakesh Sharma

On Jan 6, 12:08 am, markhob...@hotpop.donottypethisbit.com (Mark
Hobley) wrote:
> I have some variable foo, and I was expecting to be able to compare it
> to a literal string "bar", so I coded:
>
> if [ $foo = bar ] ; then
> echo 'Ok, they are the same.'
> fi
>
> If foo is null, I get an error "unexpected operator". So I can't compare
> a string if it has a null value.
>
> I now try:
>
> if [ -n $foo ] ; then
> if [ $foo = bar ] ; then
> echo 'Ok, they are the same.'
> fi
> fi
>
> I am still getting the error, presumably because the first (outermost)
> if conditional statement falls through because $foo is null.
>
> What should I be doing here to compare $foo to the literal string "bar"?
>


the 'case' construct of the bourne shell is the best here:

case ${foo-} in
'bar' )
echo 'foo equals bar'
;;
esac