Skip to contentSkip to conversation

HUSH logical AND/OR expressions

U-Boot Joakim Tjernlund 4 participants 8 messages Apr 27, 2015 – Apr 29, 2015

lore
Share & tools
lore
#0 lore raw

Trying to get a better handle of HUSH shell expressions, this does not work as I expect:
=> false && true || echo ECHO
=> false && false || echo ECHO

none prints ECHO, seems like a bug?

This the only one that prints ECHO

Quoted 1 line

= true && false || echo ECHO

Jocke

#1 lore raw

Hi Joakim,

Quoted 5 lines · Joakim Tjernlund

Trying to get a better handle of HUSH shell expressions, this does not work as I expect:
=> false && true || echo ECHO
=> false && false || echo ECHO

none prints ECHO, seems like a bug?

I think it works as it should. false followed by && will terminate always.

Quoted 2 lines

This the only one that prints ECHO

Quoted 1 line

= true && false || echo ECHO

This also seems correct. Passing true into && and false into || will always continue.

Cheers,
-Joe

#2 lore raw

Dear Joe Hershberger,

Quoted 10 lines · Joe Hershberger

Hi Joakim,

Quoted 5 lines · Joakim Tjernlund

Trying to get a better handle of HUSH shell expressions, this does not work as I expect:
=> false && true || echo ECHO
=> false && false || echo ECHO

none prints ECHO, seems like a bug?

Quoted 8 lines

I think it works as it should. false followed by && will terminate always.

Quoted 2 lines

This the only one that prints ECHO

Quoted 1 line

= true && false || echo ECHO

This also seems correct. Passing true into && and false into || will always continue.

I thought hush is supposed to be mostly similar to sh. On my Linux desktop, bash (which is supposed to be backward compatible with sh) says

$ which sh
/usr/bin/sh
$ ls -l /usr/bin/sh
lrwxrwxrwx 1 root root 4 Apr 17 14:43 /usr/bin/sh -> bash
$ sh --version
GNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
<http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.

$ sh
sh-4.2$ false && true || echo ECHO
ECHO
sh-4.2$ false && false || echo ECHO
ECHO
sh-4.2$ true && false || echo ECHO
ECHO
sh-4.2$ exit
exit

Is this one of the places where hush and sh are not the same?

Regards,
Jim

Signature · 3 lines
Jim Chargin
AJA Video Systems                       jimc at aja.com
(530) 271-3334                          http://www.aja.com
#3 lore raw

Hi James,

Quoted 53 lines · James Chargin

Dear Joe Hershberger,

Quoted 16 lines · Joe Hershberger

Hi Joakim,

Quoted 7 lines · Joakim Tjernlund

Trying to get a better handle of HUSH shell expressions, this does not work as I expect:
=> false && true || echo ECHO
=> false && false || echo ECHO

none prints ECHO, seems like a bug?

I think it works as it should. false followed by && will terminate always.

Quoted 8 lines
Quoted 3 lines

This the only one that prints ECHO

Quoted 2 lines

= true && false || echo ECHO

This also seems correct. Passing true into && and false into || will always continue.

I thought hush is supposed to be mostly similar to sh. On my Linux desktop, bash (which is supposed to be backward compatible with sh) says

$ which sh
/usr/bin/sh
$ ls -l /usr/bin/sh
lrwxrwxrwx 1 root root 4 Apr 17 14:43 /usr/bin/sh -> bash
$ sh --version
GNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
<http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.

$ sh
sh-4.2$ false && true || echo ECHO
ECHO
sh-4.2$ false && false || echo ECHO
ECHO
sh-4.2$ true && false || echo ECHO
ECHO
sh-4.2$ exit
exit
Quoted 1 line

Is this one of the places where hush and sh are not the same?

The way hush seems to work is it drops out of the entire command if a case is false.

Looking at the code in the hush parser, it seems at face value like it should be skipping the command and then picking up the next command (echo), but it clearly doesn't.

if ( (rcode==EXIT_SUCCESS && pi->followup==PIPE_OR) ||

     (rcode!=EXIT_SUCCESS && pi->followup==PIPE_AND) )
        skip_more_in_this_rmode=rmode;

I always just assumed this was a limitation of hush, but it's now looking like a bug instead.

I haven't debugged into it, but I tend to stay away from hush these days. When I've submitted a bug fix to this area I've been told that we should upgrade wholesale instead, and I've haven't been up for that task so far. http://lists.denx.de/pipermail/u-boot/2012-November/139841.html

Perhaps there is an easy, minor bugfix for this that would be palette-able.

-Joe

#4 lore raw

Dear Joe,

Quoted 16 lines · Joe Hershberger

Hi James,

Quoted 13 lines · James Chargin

Dear Joe Hershberger,

Quoted 10 lines · Joe Hershberger

Hi Joakim,

Quoted 5 lines · Joakim Tjernlund

Trying to get a better handle of HUSH shell expressions, this does not work as I expect:
=> false && true || echo ECHO
=> false && false || echo ECHO

...

Quoted 8 lines

I always just assumed this was a limitation of hush, but it's now looking like a bug instead.

I haven't debugged into it, but I tend to stay away from hush these days. When I've submitted a bug fix to this area I've been told that we should upgrade wholesale instead, and I've haven't been up for that task so far. http://lists.denx.de/pipermail/u-boot/2012-November/139841.html

That's a great conversation with Wolfgang, thanks for the reference.

Like you, I have tended to use hush carefully. It is similar to sh/bash, but different enough that I never count on an easy port of bash script fragments.

I noticed you asked for a reference to the original hush code that is the origin for U-Boot's adaptation. I couldn't find a reply to that question. I also haven't been able to find anything with a quick we search. Do you know where it did come from?

Quoted 2 lines

Perhaps there is an easy, minor bugfix for this that would be palette-able.

I've been "afraid" of the hush code for a while. I don't think I want to mess with it now. I'll just continue to work with what we've got, carefully.

Thanks again for your attention,

Jim

#5 lore raw

Hi James,

Quoted 41 lines · James Chargin

Dear Joe,

Quoted 21 lines · Joe Hershberger

Hi James,

Quoted 16 lines · James Chargin

Dear Joe Hershberger,

Quoted 12 lines · Joe Hershberger

Hi Joakim,

Quoted 6 lines · Joakim Tjernlund

Trying to get a better handle of HUSH shell expressions, this does not work as I expect:
=> false && true || echo ECHO
=> false && false || echo ECHO

...

Quoted 9 lines

I always just assumed this was a limitation of hush, but it's now looking like a bug instead.

I haven't debugged into it, but I tend to stay away from hush these days. When I've submitted a bug fix to this area I've been told that we should upgrade wholesale instead, and I've haven't been up for that task so far.
http://lists.denx.de/pipermail/u-boot/2012-November/139841.html

That's a great conversation with Wolfgang, thanks for the reference.

Quoted 8 lines

Like you, I have tended to use hush carefully. It is similar to sh/bash, but different enough that I never count on an easy port of bash script fragments.

I noticed you asked for a reference to the original hush code that is the origin for U-Boot's adaptation. I couldn't find a reply to that question. I also haven't been able to find anything with a quick we search. Do you know where it did come from?

I never got a response, but it appears to be busybox...

http://git.busybox.net/busybox/tree/shell/hush.c

Quoted 9 lines
Quoted 3 lines

Perhaps there is an easy, minor bugfix for this that would be palette-able.

I've been "afraid" of the hush code for a while. I don't think I want to mess with it now. I'll just continue to work with what we've got, carefully.

Thanks again for your attention,

No problem...

Cheers,
-Joe

#6 lore raw

Dear Joe,

Quoted 7 lines · In message <CANr=Z=YxsT-VOje5jA0k00BSxNROXBOPXMG-Q_8_7jAYN=fJCw@mail.gmail.com>…
Quoted 5 lines

I noticed you asked for a reference to the original hush code that is the origin for U-Boot's adaptation. I couldn't find a reply to that question. I also haven't been able to find anything with a quick we search. Do you know where it did come from?

I never got a response, but it appears to be busybox...

Oops, sorry. Yes, of course it is busybox.

As far as I can tell, it was added around October 2001 to the PPCBoot project as part of the PPCBoot v1.1.0 release:

* Added "hush" shell (from Busybox) as alternative command line interpreter, thus enabling powerful command line syntax like if...then...else...fi conditionals or `&&' and '||' constructs ("shell scripts").

See the README file for information about implementation and usage.

Best regards,

Wolfgang Denk

Signature · 7 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Winners never talk about glorious victories. That's  because  they're
the  ones  who  see  what the battlefield looks like afterwards. It's
only the losers who have glorious victories.
                                      - Terry Pratchett, _Small Gods_
#7 lore raw

Wolfgang,

Thanks for your contributions.

Quoted 16 lines · Wolfgang Denk

Dear Joe,

Quoted 7 lines · In message <CANr=Z=YxsT-VOje5jA0k00BSxNROXBOPXMG-Q_8_7jAYN=fJCw@mail.gmail.com>…
Quoted 5 lines

I noticed you asked for a reference to the original hush code that is the origin for U-Boot's adaptation. I couldn't find a reply to that question. I also haven't been able to find anything with a quick we search. Do you know where it did come from?

I never got a response, but it appears to be busybox...

Oops, sorry. Yes, of course it is busybox.

As far as I can tell, it was added around October 2001 to the PPCBoot project as part of the PPCBoot v1.1.0 release:

Quoted 7 lines

* Added "hush" shell (from Busybox) as alternative command line interpreter, thus enabling powerful command line syntax like if...then...else...fi conditionals or `&&' and '||' constructs ("shell scripts").

See the README file for information about implementation and usage.

I should have thought of this myself. I could have looked in the commit history or release notes.

Just for completeness, I tried the commands using busybox:

# busybox|head -1
BusyBox v1.20.2 (2013-07-02 15:49:59 PDT) multi-call binary.

# false && true || echo ECHO
ECHO
# false && false || echo ECHO
ECHO
# true && false || echo ECHO
ECHO
#
Signature · 3 lines
Jim Chargin
AJA Video Systems                       jimc at aja.com
(530) 271-3334                          http://www.aja.com