PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > Comparing two strings (one may be a null)
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Comparing two strings (one may be a null)

Réponse
 
LinkBack Outils de la discussion
Vieux 05/01/2008, 19h08   #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
Vieux 05/01/2008, 19h13   #2
Cyrus Kriticos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Comparing two strings (one may be a null)

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.
  Réponse avec citation
Vieux 05/01/2008, 20h50   #3
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Comparing two strings (one may be a null)

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 <http://cfaj.freeshell.org/shell/>
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
  Réponse avec citation
Vieux 05/01/2008, 22h08   #4
Mark Hobley
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Comparing two strings (one may be a null)

Cyrus Kriticos <cyrus.kriticos@googlemail.com> 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.
  Réponse avec citation
Vieux 05/01/2008, 22h29   #5
Cyrus Kriticos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Comparing two strings (one may be a null)

Mark Hobley wrote:
> Cyrus Kriticos <cyrus.kriticos@googlemail.com> 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.
  Réponse avec citation
Vieux 06/01/2008, 02h08   #6
Mark Hobley
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Comparing two strings (one may be a null)

Cyrus Kriticos <cyrus.kriticos@googlemail.com> 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.
  Réponse avec citation
Vieux 06/01/2008, 05h12   #7
Barry Margolin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Comparing two strings (one may be a null)

In article <ul8555-6k6.ln1@neptune.markhobley.yi.org>,
markhobley@hotpop.donottypethisbit.com (Mark Hobley) wrote:

> Cyrus Kriticos <cyrus.kriticos@googlemail.com> 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 ***
  Réponse avec citation
Vieux 06/01/2008, 13h15   #8
Maxwell Lol
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Comparing two strings (one may be a null)

Cyrus Kriticos <cyrus.kriticos@googlemail.com> 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....
  Réponse avec citation
Vieux 06/01/2008, 18h06   #9
kevin.hehl@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Comparing two strings (one may be a null)

On Jan 5, 12:08pm, 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"?
>
> Mark.
>
> --
> Mark Hobley,
> 393 Quinton Road West,
> Quinton, BIRMINGHAM.
> B32 1QE.


you need to quote the values

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

that way, if $foo is null, the quotes make it a 'emtpy' string
comparison.
  Réponse avec citation
Vieux 07/01/2008, 06h07   #10
Stephane Chazelas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Comparing two strings (one may be a null)

On 06 Jan 2008 08:15:27 -0500, Maxwell Lol wrote:
> Cyrus Kriticos <cyrus.kriticos@googlemail.com> 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
  Réponse avec citation
Vieux 08/01/2008, 07h53   #11
Rakesh Sharma
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Comparing two strings (one may be a null)

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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 22h26.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,20247 seconds with 19 queries