Afficher un message
Vieux 05/01/2008, 20h08   #1
Mark Hobley
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Comparing two strings (one may be a null)

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.
  Réponse avec citation
 
Page generated in 0,06006 seconds with 9 queries