zsh-users
 help / color / mirror / code / Atom feed
From: Roman Perepelitsa <roman.perepelitsa@gmail.com>
To: Ray Andrews <rayandrews@eastlink.ca>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: Re: (N) defaults to whole directory listing
Date: Fri, 29 Jan 2021 16:33:04 +0100	[thread overview]
Message-ID: <CAN=4vMp+JykOWEYQ5qO495-hOpQXh+4RN6iHcYTP-APg8XHESQ@mail.gmail.com> (raw)
In-Reply-To: <d611ab66-c6f9-655f-89dc-ded8697fe0ed@eastlink.ca>

On Fri, Jan 29, 2021 at 4:24 PM Ray Andrews <rayandrews@eastlink.ca> wrote:
>
> Is this really what we want?
>
>     $ ls -1 no_such_file
>     ls: cannot access 'no_such_file': No such file or directory
>
>     $ ls -1 no_such_file 2> /dev/null
>
>     $ ls -1 no_such_file(N)
>     aaa
>     aa-messagefunctions
>     aliases
>     aliases,1,remove _find_match
>     aliases,2,STARTCOM
>     ...
>
>     ....  lists the whole directory.  It's not exactly what one might
>     expect. I can see that if the argument 'disappears' entirely then
>     'ls' thinks 'ls *' but shouldn't 'ls' be in a 'not found' condition
>     rather than a 'list all' condition?

`ls` without arguments lists files in the current directory. This is
mandated by POSIX and what everyone expects.

`no_such_file(N)` expands to nothing if there is no file named
`no_such_file` in the current directory. This is in line with the zsh
documentation and what everyone expects.

If your examples aren't artificial but really show what you are trying
to do, you can do it like this:

  print -rC1 -- no_such_file(N)

This will print `no_such_file` if such file exists and will do nothing
otherwise.

If you really need that `ls` call (perhaps because you are passing
some flags to it), you can do this:

  ( ls -1 no_such_file(#q) ) 2>/dev/null

This does the same as above but about 1000 times slower and returns an
error if the file doesn't exist.

You can also use an explicit condition:

  if [[ -e no_such_file ]]; then
    ls -1 no_such_file
  fi

Roman.


  reply	other threads:[~2021-01-29 15:33 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-29 15:24 Ray Andrews
2021-01-29 15:33 ` Roman Perepelitsa [this message]
2021-01-29 17:01   ` Ray Andrews
2021-01-29 17:52     ` Lawrence Velázquez
2021-01-29 19:52       ` Ray Andrews
2021-01-29 18:47     ` Bart Schaefer
2021-01-29 19:04   ` Bart Schaefer
2021-01-30 10:50     ` Roman Perepelitsa
2021-01-30 15:00       ` Beverly Pope
2021-06-18 10:24     ` Why does >&- produce an error message when connected to a builtin? (was Re: (N) defaults to whole directory listing) Marlon Richert

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAN=4vMp+JykOWEYQ5qO495-hOpQXh+4RN6iHcYTP-APg8XHESQ@mail.gmail.com' \
    --to=roman.perepelitsa@gmail.com \
    --cc=rayandrews@eastlink.ca \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).