zsh-users
 help / color / mirror / code / Atom feed
* [ -f glob ]
@ 2008-01-09  9:13 Atom Smasher
  2008-01-09 12:39 ` Peter Stephenson
  0 siblings, 1 reply; 5+ messages in thread
From: Atom Smasher @ 2008-01-09  9:13 UTC (permalink / raw)
  To: zsh-users

here's a weird one...

this is part of a sanity test on line 369 of my zshrc (link below):
     [ -f /usr/{share,lib,share/lib}/{zoneinfo,locale/TZ}/${TZ} ]

here it works:	  zsh 4.2.1 (i386-portbld-freebsd4.7)
here it doesn't:  zsh 4.2.1 (sparc-sun-solaris2.10)

on _both_ systems, the command below will show errors for non-existent 
files (to stderr), and will show that a tz-file exists (to stdout). on 
both systems the command returns >0.
  % ls -l  /usr/{share,lib,share/lib}/{zoneinfo,locale/TZ}/${TZ}

this works on both bsd and solaris:
  % ls /usr/{share,lib,share/lib}/{zoneinfo,locale/TZ}/${TZ} 2> /dev/null | grep -q $TZ

i can use the return status from grep to determine if the file exists. but 
i'd prefer to keep this in zsh, as much as possible. i suppose i could 
assign stdout to a variable, test for the variable, and do it that way... 
but it seems a bit convoluted.

1) why does this behave differently with two different builds of 
zsh-4.2.1? has it likely been fixed since 4.2.1?

2) is there a simple fix? or simple workaround?

my zshrc is v0.119 - http://smasher.org/zsh/


-- 
         ...atom

  ________________________
  http://atom.smasher.org/
  762A 3B98 A3C3 96C9 C6B7 582A B88D 52E4 D9F5 7808
  -------------------------------------------------

 	"The capitalists owned everything in the world, and everyone
 	 else was their slave. They owned all the land, all the
 	 houses, all the factories, and all the money. If anyone
 	 disobeyed them they could throw him into prison, or they
 	 could take his job away and starve him to death. When any
 	 ordinary person spoke to a capitalist he had to cringe and
 	 bow to him, and take off his cap and address him as 'Sir'"
 		-- George Orwell


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [ -f glob ]
  2008-01-09  9:13 [ -f glob ] Atom Smasher
@ 2008-01-09 12:39 ` Peter Stephenson
  2008-01-10  4:48   ` Atom Smasher
  0 siblings, 1 reply; 5+ messages in thread
From: Peter Stephenson @ 2008-01-09 12:39 UTC (permalink / raw)
  To: zsh-users

Atom Smasher wrote:
> here's a weird one...
> 
> this is part of a sanity test on line 369 of my zshrc (link below):
>      [ -f /usr/{share,lib,share/lib}/{zoneinfo,locale/TZ}/${TZ} ]

This is broken: -f expectes exactly one argument and you're giving it an
expression that expands to multiple arguments.  The [ ... ] form of
tests isn't recommended anyway, because the arguments aren't parsed
properly so this sort of error will be hidden.  But it's not at all
clear what you're trying to do.  I think you're looking to see if at
least one of the files in the expansion exists, in which case try
expanding the expression and using a null glob:

files=(/usr/{share,lib,share/lib}/{zoneinfo,locale/TZ}/${TZ}(N))
if (( ${#files} )); then
  # stuff
fi

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [ -f glob ]
  2008-01-09 12:39 ` Peter Stephenson
@ 2008-01-10  4:48   ` Atom Smasher
  2008-01-10  9:58     ` Peter Stephenson
  0 siblings, 1 reply; 5+ messages in thread
From: Atom Smasher @ 2008-01-10  4:48 UTC (permalink / raw)
  To: zsh-users

On Wed, 9 Jan 2008, Peter Stephenson wrote:

> This is broken: -f expectes exactly one argument and you're giving it an 
> expression that expands to multiple arguments.
================

but, why would it work on bsd and not work on solaris?


> The [ ... ] form of tests isn't recommended anyway, because the 
> arguments aren't parsed properly so this sort of error will be hidden. 
> But it's not at all clear what you're trying to do.  I think you're 
> looking to see if at least one of the files in the expansion exists, in 
> which case try expanding the expression and using a null glob:
>
> files=(/usr/{share,lib,share/lib}/{zoneinfo,locale/TZ}/${TZ}(N))
> if (( ${#files} )); then
>  # stuff
> fi
================

very slightly modified, and working great...

  local tz_file
  tz_file=(/usr/{share,lib,share/lib}/{zoneinfo,locale/TZ}/${TZ}(.N))
  (( ${#tz_file} )) || export TZ=Etc/UTC

thanks!


-- 
         ...atom

  ________________________
  http://atom.smasher.org/
  762A 3B98 A3C3 96C9 C6B7 582A B88D 52E4 D9F5 7808
  -------------------------------------------------

 	"I believe that banking institutions are more dangerous
 	 to our liberties than standing armies."
 		-- Thomas Jefferson


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [ -f glob ]
  2008-01-10  4:48   ` Atom Smasher
@ 2008-01-10  9:58     ` Peter Stephenson
  2008-01-10 12:06       ` Peter Stephenson
  0 siblings, 1 reply; 5+ messages in thread
From: Peter Stephenson @ 2008-01-10  9:58 UTC (permalink / raw)
  To: zsh-users

Atom Smasher wrote:
> On Wed, 9 Jan 2008, Peter Stephenson wrote:
> 
> > This is broken: -f expectes exactly one argument and you're giving it an 
> > expression that expands to multiple arguments.
> ================
> 
> but, why would it work on bsd and not work on solaris?

You're into implementation-specific land, from whose Bourne shell no
traveller returns.

Schematically, you've got something like

[ -f file1 file2 file3 ]

(number not that important).  The "-f file1" is a valid test.  At the
file2 I would expect the test implementation simply to give up and
return an error, but there is so much hackery to get round the fact that
keywords in the test aren't syntactically marked and that the default
test is for a non-empty string even, in some cases, if the string looks
like a keyword that I don't think it's worth worrying about, just
noting that "[" and "test" mean trouble.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [ -f glob ]
  2008-01-10  9:58     ` Peter Stephenson
@ 2008-01-10 12:06       ` Peter Stephenson
  0 siblings, 0 replies; 5+ messages in thread
From: Peter Stephenson @ 2008-01-10 12:06 UTC (permalink / raw)
  To: zsh-users

On Thu, 10 Jan 2008 09:58:02 +0000
Peter Stephenson <pws@csr.com> wrote:
> Atom Smasher wrote:
> > On Wed, 9 Jan 2008, Peter Stephenson wrote:
> > 
> > > This is broken: -f expectes exactly one argument and you're giving it an 
> > > expression that expands to multiple arguments.
> > ================
> > 
> > but, why would it work on bsd and not work on solaris?
> 
> You're into implementation-specific land, from whose Bourne shell no
> traveller returns.

However, zsh isn't helping by not reporting an error if there are arguments
it couldn't parse.

Index: Src/builtin.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/builtin.c,v
retrieving revision 1.184
diff -u -r1.184 builtin.c
--- Src/builtin.c	17 Dec 2007 17:11:29 -0000	1.184
+++ Src/builtin.c	10 Jan 2008 12:04:28 -0000
@@ -5472,7 +5472,7 @@
 
 /* holds arguments for testlex() */
 /**/
-char **testargs;
+char **testargs, **curtestarg;
 
 /* test, [: the old-style general purpose logical expression builtin */
 
@@ -5483,7 +5483,7 @@
     if (tok == LEXERR)
 	return;
 
-    tokstr = *testargs;
+    tokstr = *(curtestarg = testargs);
     if (!*testargs) {
 	/* if tok is already zero, reading past the end:  error */
 	tok = tok ? NULLTOK : LEXERR;
@@ -5557,6 +5557,11 @@
 	return 1;
     }
 
+    if (*curtestarg) {
+	zwarnnam(name, "too many arguments");
+	return 1;
+    }
+
     /* syntax is OK, so evaluate */
 
     state.prog = prog;
Index: Src/parse.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/parse.c,v
retrieving revision 1.67
diff -u -r1.67 parse.c
--- Src/parse.c	27 Jun 2007 13:56:11 -0000	1.67
+++ Src/parse.c	10 Jan 2008 12:04:32 -0000
@@ -537,6 +537,12 @@
     return bld_eprog();
 }
 
+/*
+ * This entry point is only used for bin_test, our attempt to
+ * provide compatibility with /bin/[ and /bin/test.  Hence
+ * at this point condlex should always be set to testlex.
+ */
+
 /**/
 mod_export Eprog
 parse_cond(void)
Index: Test/C02cond.ztst
===================================================================
RCS file: /cvsroot/zsh/zsh/Test/C02cond.ztst,v
retrieving revision 1.20
diff -u -r1.20 C02cond.ztst
--- Test/C02cond.ztst	4 Jan 2008 14:45:40 -0000	1.20
+++ Test/C02cond.ztst	10 Jan 2008 12:04:33 -0000
@@ -199,7 +199,31 @@
 0:strings with `[' builtin
 
   [ `echo 0` -lt `echo 1` ]
-0:substituion in `[' builtin
+0:substitution in `[' builtin
+
+  [ -n foo scrimble ]
+1:argument checking for [ builtin
+?(eval):[:1: too many arguments
+
+  test -n foo scramble
+1:argument checking for test builtin
+?(eval):test:1: too many arguments
+
+  [ -n foo scrimble scromble ]
+1:argument checking for [ builtin
+?(eval):[:1: too many arguments
+
+  test -n foo scramble scrumble
+1:argument checking for test builtin
+?(eval):test:1: too many arguments
+
+  [ -n foo -a -n bar scrimble ]
+1:argument checking for [ builtin
+?(eval):[:1: too many arguments
+
+  test -n foo -a -z "" scramble
+1:argument checking for test builtin
+?(eval):test:1: too many arguments
 
   fn() {
     # careful: first file must exist to trigger bug

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2008-01-10 18:33 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-01-09  9:13 [ -f glob ] Atom Smasher
2008-01-09 12:39 ` Peter Stephenson
2008-01-10  4:48   ` Atom Smasher
2008-01-10  9:58     ` Peter Stephenson
2008-01-10 12:06       ` Peter Stephenson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).