Comparing two strings (one may be a null)
- From: markhobley@xxxxxxxxxxxxxxxxxxxxxxxxxxx (Mark Hobley)
- Date: Sat, 05 Jan 2008 19:08:05 GMT
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.
.
- Follow-Ups:
- Re: Comparing two strings (one may be a null)
- From: Rakesh Sharma
- Re: Comparing two strings (one may be a null)
- From: kevin . hehl
- Re: Comparing two strings (one may be a null)
- From: Chris F.A. Johnson
- Re: Comparing two strings (one may be a null)
- From: Cyrus Kriticos
- Re: Comparing two strings (one may be a null)
- Prev by Date: Re: Why mv have no '-R'
- Next by Date: Re: Comparing two strings (one may be a null)
- Previous by thread: Why mv have no '-R'
- Next by thread: Re: Comparing two strings (one may be a null)
- Index(es):
Relevant Pages
|
|