Skip to contentSkip to conversation

Use of == in shell scripts

DISCUSSION Andreas Oberritter 5 participants 11 messages Oct 26, 2010 – Nov 14, 2010

lore
Share & tools
lore
#0 lore raw

Dear all,

I noticed a common mistake in various OE recipes, classes and contrib
scripts: Instead of =, == is used as equality operator, in at least 91 places in 64 files (without recipes/obsolete: 83 in 60 files). This isn't a problem with bash, but it isn't the standard syntax and at least dash does not support it.

I used the following command line to find the occurences:

git grep 'if\s*\[.*==.*\]'

This command fixes all occurences:

for i in git grep -l 'if\s*\[.*==.*\]'; do

	sed -i $i -e 's,\(if\s*\[.*\)==\(.*\]\),\1=\2,';

done

One of the matches is a false positive:
recipes/uclibc/uclibc-0.9.29/uClibc-0.9.29-nonposix_bashisms.patch

How do I submit such a patch? 60 patches may be too much for such a relatively simple change, but, on the other hand, everything in a single patch may be unhandy as well.

How about splitting it into 5 patches?

- classes (7 files)
- contrib (4 files)
- recipes/*/*.bb (24 files)
- recipes/*/*.inc (14 files)
- other files in recipes except recipes/obsolete (11 files)

Any opinions?

Regards,
Andreas

#1 lore raw
Quoted 7 lines · Andreas Oberritter

How about splitting it into 5 patches?

- classes (7 files)
- contrib (4 files)
- recipes/*/*.bb (24 files)
- recipes/*/*.inc (14 files)
- other files in recipes except recipes/obsolete (11 files)

Or how about those 37 patches?

http://git.opendreambox.org/?p=obi/openembedded.git;a=shortlog;h=refs/heads/equality-operator

Rationale:
- bbclasses: 1 patch per file
- contrib and recipes: 1 patch per directory

Regards,
Andreas

#7 lore raw
In reply to Andreas Oberritter
Quoted 8 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Andreas Oberritter
Sent: Tuesday, October 26, 2010 10:31 AM
To: [email protected]
Subject: [oe] Use of == in shell scripts

Quoted 7 lines

Dear all,

I noticed a common mistake in various OE recipes, classes and contrib
scripts: Instead of =, == is used as equality operator, in at least 91 places in 64 files (without recipes/obsolete: 83 in 60 files). This isn't a problem with bash, but it isn't the standard syntax and at least dash does not support it.

Isn't bash the supported shell? If I remember correctly in bash "==" will also work with numbers whereas "=" is only for strings.

Quoted 35 lines

I used the following command line to find the occurences:

git grep 'if\s*\[.*==.*\]'

This command fixes all occurences:

for i in git grep -l 'if\s*\[.*==.*\]'; do

	sed -i $i -e 's,\(if\s*\[.*\)==\(.*\]\),\1=\2,';

done

One of the matches is a false positive:
recipes/uclibc/uclibc-0.9.29/uClibc-0.9.29-nonposix_bashisms.patch

How do I submit such a patch? 60 patches may be too much for such a relatively simple change, but, on the other hand, everything in a single patch may be unhandy as well.

How about splitting it into 5 patches?

- classes (7 files)
- contrib (4 files)
- recipes/*/*.bb (24 files)
- recipes/*/*.inc (14 files)
- other files in recipes except recipes/obsolete (11 files)

Any opinions?

Regards,
Andreas

_______________________________________________
Openembedded-devel mailing list
[email protected]
http://lists.linuxtogo.org/cgi-bin/mailman/listinfo/openembedded-devel

#8 lore raw
Quoted 10 lines · Chase
Quoted 10 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Andreas Oberritter
Sent: Tuesday, October 26, 2010 10:31 AM
To: [email protected]
Subject: [oe] Use of == in shell scripts

Dear all,

Quoted 8 lines
Quoted 5 lines

I noticed a common mistake in various OE recipes, classes and contrib
scripts: Instead of =, == is used as equality operator, in at least 91 places in 64 files (without recipes/obsolete: 83 in 60 files). This isn't a problem with bash, but it isn't the standard syntax and at least dash does not support it.

Isn't bash the supported shell? If I remember correctly in bash "==" will also work with numbers whereas "=" is only for strings.

For now, it's required, but not by our choice. We were forced to it due to portability issues with scripts in the buildsystems of things we build.

Signature · 5 lines
Christopher Larson
clarson at kergoth dot com
Founder - BitBake, OpenEmbedded, OpenZaurus
Maintainer - Tslib
Senior Software Engineer, Mentor Graphics
#9 lore raw
In reply to Maupin, Chase
Quoted 1 line · Chase

Isn't bash the supported shell?

Yes, at least on the build system. Some of the affected files are executed on the target.

I understand that bash is a requirement for now, but if possible, that should be changed. I don't think that root access to a machine should be necessary to build OE on Ubuntu (or maybe even on BSD flavors). Does Darwin come with bash?

Btw., I discovered the first occurence of this problem with bash on Debian lenny, where wpa-supplicant unconditionally enabled madwifi support, because == was used. Maybe under some circumstances the bash-builtins of '[' and 'test' are not used.

Quoted 1 line

If I remember correctly in bash "==" will also work with numbers whereas "=" is only for strings.

Both bash and dash on my system handle "[ "0" = 0 ]" or [ "0" = 1 ] correctly. Another operator "-eq" exists, which is only for numbers.

Regards,
Andreas

#10 lore raw
Quoted 5 lines · Andreas Oberritter

On 10/26/2010 05:51 PM, Maupin, Chase wrote:

I understand that bash is a requirement for now, but if possible, that should be changed. I don't think that root access to a machine should be necessary to build OE on Ubuntu (or maybe even on BSD flavors).

Well, maybe Ubuntu should be changed to use bash :)

Quoted 4 lines
Quoted 1 line

If I remember correctly in bash "==" will also work with numbers whereas "=" is only for strings.

Both bash and dash on my system handle "[ "0" = 0 ]" or [ "0" = 1 ] correctly. Another operator "-eq" exists, which is only for numbers.

From what I can see, it looks like = and == are equivalent in bash, so we should use = so we're not tied to bash.

-eq is for numeric comparison. For example, [ 01 -eq 1 ] but not [ "01" = "1" ].

Mike

Acked-by
#2 lore raw
In reply to Andreas Oberritter
Quoted 9 lines · Andreas Oberritter
Quoted 7 lines · Andreas Oberritter

How about splitting it into 5 patches?

- classes (7 files)
- contrib (4 files)
- recipes/*/*.bb (24 files)
- recipes/*/*.inc (14 files)
- other files in recipes except recipes/obsolete (11 files)

Quoted 7 lines

Or how about those 37 patches?

http://git.opendreambox.org/?p=obi/openembedded.git;a=shortlog;h=refs/heads/equality-operator

Rationale:
- bbclasses: 1 patch per file
- contrib and recipes: 1 patch per directory

For the series:

Except this one:

http://git.opendreambox.org/?p=obi/openembedded.git;a=commitdiff;h=ad7b2c9ab0c305034c39d2efc5a52789965bb5c2

where some lines had == twice, looks like your sed may not have caught it.

BTW, some of the files have constructs like this:

if [ "x$HAS_MADWIFI" = "x1" ]

I think this "x" business is for old broken shells that can't handle an empty quoted string as the first argument. Does anyone know the standard well enough to say? I figure [ "" = "1" ] should be legal everywhere. This is just a pet peeve/bike shed on my part.

Mike

#3 lore raw
Quoted 21 lines · Michael Smith
Quoted 9 lines · Andreas Oberritter
Quoted 7 lines · Andreas Oberritter

How about splitting it into 5 patches?

- classes (7 files)
- contrib (4 files)
- recipes/*/*.bb (24 files)
- recipes/*/*.inc (14 files)
- other files in recipes except recipes/obsolete (11 files)

Quoted 8 lines

Or how about those 37 patches?

http://git.opendreambox.org/?p=obi/openembedded.git;a=shortlog;h=refs/heads/equality-operator

Rationale:
- bbclasses: 1 patch per file
- contrib and recipes: 1 patch per directory

For the series:

Quoted 8 lines

Except this one:

http://git.opendreambox.org/?p=obi/openembedded.git;a=commitdiff;h=ad7b2c9ab0c305034c39d2efc5a52789965bb5c2

where some lines had == twice, looks like your sed may not have caught it.

Thanks for spotting! I've updated the patch series and added your ack.

Quoted 8 lines

BTW, some of the files have constructs like this:

if [ "x$HAS_MADWIFI" = "x1" ]

I think this "x" business is for old broken shells that can't handle an empty quoted string as the first argument. Does anyone know the standard well enough to say? I figure [ "" = "1" ] should be legal everywhere. This is just a pet peeve/bike shed on my part.

Autotools use constructs like that everywhere, so it's likely increasing portability while decreasing readability.

A random forum post suggests that some versions of '[' have problems, if the first operand equals '!' or '(' [1].

It's not easy to search for [ or test or x on google. ;-)

Regards,
Andreas

[1]
http://www.techtalkz.com/unix/78138-testing-equality-bash-scripts.html#post329122

#4 lore raw
Quoted 42 lines · Andreas Oberritter
Quoted 21 lines · Michael Smith
Quoted 9 lines · Andreas Oberritter
Quoted 7 lines · Andreas Oberritter

How about splitting it into 5 patches?

- classes (7 files)
- contrib (4 files)
- recipes/*/*.bb (24 files)
- recipes/*/*.inc (14 files)
- other files in recipes except recipes/obsolete (11 files)

Quoted 8 lines

Or how about those 37 patches?

http://git.opendreambox.org/?p=obi/openembedded.git;a=shortlog;h=refs/heads/equality-operator

Rationale:
- bbclasses: 1 patch per file
- contrib and recipes: 1 patch per directory

For the series:

Quoted 8 lines

Except this one:

http://git.opendreambox.org/?p=obi/openembedded.git;a=commitdiff;h=ad7b2c9ab0c305034c39d2efc5a52789965bb5c2

where some lines had == twice, looks like your sed may not have caught it.

Thanks for spotting! I've updated the patch series and added your ack.

Quoted 8 lines

BTW, some of the files have constructs like this:

if [ "x$HAS_MADWIFI" = "x1" ]

I think this "x" business is for old broken shells that can't handle an empty quoted string as the first argument. Does anyone know the standard well enough to say? I figure [ "" = "1" ] should be legal everywhere. This is just a pet peeve/bike shed on my part.

Quoted 5 lines

Autotools use constructs like that everywhere, so it's likely increasing portability while decreasing readability.

A random forum post suggests that some versions of '[' have problems, if the first operand equals '!' or '(' [1].

And the less random Autoconf manual has this to say about the shell builtin, test, and it's more common name '[':

"Similarly, Posix says that both ‘test "string1" = "string2"’ and ‘test "string1" != "string2"’ work for any pairs of strings, but in practice this is not true for troublesome strings that look like operators or parentheses, or that begin with ‘-’.

It is best to protect such strings with a leading ‘X’, e.g., ‘test "Xstring" != X’ rather than ‘test -n "string"’ or ‘test ! "string"’."[2]

See the discussion of 'test(string)' at the link below.

[2] http://www.gnu.org/software/autoconf/manual/autoconf.html#Limitations-of-Builtins

Quoted 8 lines

It's not easy to search for [ or test or x on google. ;-)

Regards,
Andreas

[1]
http://www.techtalkz.com/unix/78138-testing-equality-bash-scripts.html#post329122