|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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 *** |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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.... |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|