Use of == in shell scripts
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
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
Michael Smith
Acked-byQuoted 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:
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
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 directoryFor the series:
Quoted 8 lines
Acked-by Michael Smith <[email protected]>Except this one:
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
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 directoryFor the series:
Quoted 8 lines
Acked-by Michael Smith <[email protected]>Except this one:
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
Hello Michael,
Quoted 1 line · Andreas Oberritter
Quoted 1 line
can you please pull and push these patches? I've rebased the tree on today's master. My write access still isn't working.
The patch to recipes/linux/linux-openmoko.inc was dropped, because it became obsolete in the meantime.
Regards,
Andreas
Quoted 5 lines · Andreas Oberritter
Quoted 1 line · Andreas Oberritter
Quoted 1 line
can you please pull and push these patches? I've rebased the tree on today's master. My write access still isn't working.
Pushed, thanks. I bumped PRs for a couple of recipes where the fix was in a file that gets packaged.
Mike
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*\[.*==.*\]'; dosed -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.patchHow 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
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 scriptsDear 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
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
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