[ ! ! hey ] -> [: too many arguments

2022-06-23 Thread Steffen Nurpmeso
Hello.

I stumbled over a bug in the MUA i maintain, and when checking
against what shells do with lonely unary ! i then came to this:

  #?0|kent:tmp$ dash -c 'if [ ! ! hey ]; then echo du; fi'
  du
  #?0|kent:tmp$ bash -c 'if [ ! ! hey ]; then echo du; fi'
  bash: line 1: [: too many arguments
  #?0|kent:tmp$ busybox.static sh -c 'if [ ! ! hey ]; then echo du; fi'
  du

(The single ! that my MUA bails on seems to be handled
identically by shells.)

--steffen
|
|Der Kragenbaer,The moon bear,
|der holt sich munter   he cheerfully and one by one
|einen nach dem anderen runter  wa.ks himself off
|(By Robert Gernhardt)



Re: [ ! ! hey ] -> [: too many arguments

2022-06-23 Thread Steffen Nurpmeso
Steffen Nurpmeso wrote in
 <20220623133946.h72dj%stef...@sdaoden.eu>:
 ...
 |  #?0|kent:tmp$ bash -c 'if [ ! ! hey ]; then echo du; fi'
 |  bash: line 1: [: too many arguments

(5.1.16 that is on CRUX ports/core/bash/Pkgfile

./configure \
--prefix=/usr \
--exec-prefix= \
--disable-nls \
--with-curses \
--with-installed-readline
make -j1)

--steffen
|
|Der Kragenbaer,The moon bear,
|der holt sich munter   he cheerfully and one by one
|einen nach dem anderen runter  wa.ks himself off
|(By Robert Gernhardt)



Re: [ ! ! hey ] -> [: too many arguments

2022-06-23 Thread Chet Ramey

On 6/23/22 9:39 AM, Steffen Nurpmeso wrote:

Hello.

I stumbled over a bug in the MUA i maintain, and when checking
against what shells do with lonely unary ! i then came to this:

   #?0|kent:tmp$ dash -c 'if [ ! ! hey ]; then echo du; fi'
   du
   #?0|kent:tmp$ bash -c 'if [ ! ! hey ]; then echo du; fi'
   bash: line 1: [: too many arguments
   #?0|kent:tmp$ busybox.static sh -c 'if [ ! ! hey ]; then echo du; fi'
   du


Thanks for the report. This appears to have existed for over thirty years,
and you're the first one to report it.

Chet

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/



Re: [ ! ! hey ] -> [: too many arguments

2022-06-23 Thread Steffen Nurpmeso
Chet Ramey wrote in
 <4169f2cd-d81c-29b7-918c-156b447c5...@case.edu>:
 |On 6/23/22 9:39 AM, Steffen Nurpmeso wrote:
 |> I stumbled over a bug in the MUA i maintain, and when checking
 |> against what shells do with lonely unary ! i then came to this:
 |> 
 |>#?0|kent:tmp$ dash -c 'if [ ! ! hey ]; then echo du; fi'
 |>du
 |>#?0|kent:tmp$ bash -c 'if [ ! ! hey ]; then echo du; fi'
 |>bash: line 1: [: too many arguments
 |>#?0|kent:tmp$ busybox.static sh -c 'if [ ! ! hey ]; then echo du; fi'
 |>du
 |
 |Thanks for the report. This appears to have existed for over thirty years,
 |and you're the first one to report it.

Hey.  For a C programmer things like

  bash -c 'if [ ! hey = hey ]; then echo du; fi'
or even
  bash -c 'if [ ! ! hey = hey ]; then echo du; fi'
  bash -c 'if [ ! false = 1 ]; then echo du; fi'

seem often more strange .. and is handled the same by all above
shells.

(P.S.: but mind you, the times when i used to say, give it to me,
if i cannot break it that is a good sign, they have long passed.
You have to walk to stumble that is.
Ciao.)

--steffen
|
|Der Kragenbaer,The moon bear,
|der holt sich munter   he cheerfully and one by one
|einen nach dem anderen runter  wa.ks himself off
|(By Robert Gernhardt)



Re: [ ! ! hey ] -> [: too many arguments

2022-06-23 Thread Chet Ramey

On 6/23/22 5:08 PM, Steffen Nurpmeso wrote:

Chet Ramey wrote in
  <4169f2cd-d81c-29b7-918c-156b447c5...@case.edu>:
  |On 6/23/22 9:39 AM, Steffen Nurpmeso wrote:
  |> I stumbled over a bug in the MUA i maintain, and when checking
  |> against what shells do with lonely unary ! i then came to this:
  |>
  |>#?0|kent:tmp$ dash -c 'if [ ! ! hey ]; then echo du; fi'
  |>du
  |>#?0|kent:tmp$ bash -c 'if [ ! ! hey ]; then echo du; fi'
  |>bash: line 1: [: too many arguments
  |>#?0|kent:tmp$ busybox.static sh -c 'if [ ! ! hey ]; then echo du; fi'
  |>du
  |
  |Thanks for the report. This appears to have existed for over thirty years,
  |and you're the first one to report it.

Hey.  For a C programmer things like

   bash -c 'if [ ! hey = hey ]; then echo du; fi'
or even
   bash -c 'if [ ! ! hey = hey ]; then echo du; fi'
   bash -c 'if [ ! false = 1 ]; then echo du; fi'

seem often more strange .. and is handled the same by all above
shells.


Sure. This one isn't a common idiom for shell programmers, apparently.


--
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/



Re: [ ! ! hey ] -> [: too many arguments

2022-06-23 Thread Emanuele Torre
On 22/06/23 11:08 PM, Steffen Nurpmeso wrote:
> or even
>  bash -c 'if [ ! ! hey = hey ]; then echo du; fi'
>  bash -c 'if [ ! false = 1 ]; then echo du; fi'

`! ! hey = hey' is the same as `! ( ! ( hey = hey ) )', so
"not not true => true".

It probably works correctly because `! ! hey = hey' are five terms.

The bug only occurs for 3 or 4 terms tests:

bash-5.1$ test ! ! hey ; printf %s\\n "$?"
bash: test: too many arguments
2

bash-5.1$ test ! ! ! hey   ; printf %s\\n "$?"
bash: test: too many arguments
2

bash-5.1$ test ! ! ! ! hey ; printf %s\\n "$?"
0

bash-5.1$ test ! ! ! ! ! hey   ; printf %s\\n "$?"
1

test(1) needs to treat the 0 to 4 arguments expressions specially
according to POSIX so the bug is probably only in the code that
implements those special cases.

 -emanuele6



Re: [ ! ! hey ] -> [: too many arguments

2022-06-23 Thread Greg Wooledge
On Thu, Jun 23, 2022 at 05:58:23PM -0400, Chet Ramey wrote:
> On 6/23/22 5:08 PM, Steffen Nurpmeso wrote:
> >bash -c 'if [ ! ! hey = hey ]; then echo du; fi'

> Sure. This one isn't a common idiom for shell programmers, apparently.

I've seen it used in a math context, inside PS1.  Like so:

color=(
  "$(tput setaf 2)"  # 0 = green
  "$(tput setaf 1)"  # 1 = red
)
normal="$(tput sgr0)"
PS1='\[${color[!!$?]}\]$?\[$normal\] \h:\w\$ '

This writes the previous command's exit status in either green or red.
It "smashes" all nonzero exit codes to 1, using a double negation in
a C-style arithmetic context, in order to generate an array index for
the color.

I don't recall ever seeing it used in the way Steffen showed, though.