Skip to content

Failed wildcards still execute #2394

Closed
Closed

Description

@fizzfaldt

From the documentation:
http://fishshell.com/docs/current/#expand-wildcard

If none of the wildcarded arguments sent to a command result in any matches, the command will not be executed. If this happens when using the shell interactively, a warning will also be printed.
It still gets executed after being deleted.
This used to work for me, it might be related to my fish version.

fizzfaldt@buk ~/fishtest> fish --version
fish, version 2.2.0
fizzfaldt@buk ~/fishtest> dpkg -l | grep fish
ii  fish                                                        2.2.0-1~trusty                                      amd64        friendly interactive shell

Create a directory containing "foo" and "bar"

fizzfaldt@buk ~/fishtest> command /bin/ls -1 -l
total 0
-rw-rw-r-- 1 fizzfaldt fizzfaldt 0 Sep 16 00:38 bar
-rw-rw-r-- 1 fizzfaldt fizzfaldt 0 Sep 16 00:38 foo

If none of the wildcarded arguments sent to a command result in any matches, the command will not be executed.

This fails.. it expands the wildcards to zero arguments, but it still EXECUTES the command:

fizzfaldt@buk ~/fishtest> command /bin/ls -1 -l c*
No matches for wildcard “c*”.
fish: command /bin/ls -1 -l c*
                            ^
total 0
-rw-rw-r-- 1 fizzfaldt fizzfaldt 0 Sep 16 00:38 bar
-rw-rw-r-- 1 fizzfaldt fizzfaldt 0 Sep 16 00:38 foo
fizzfaldt@buk ~/fishtest>

Note that if no matches are found for a specific wildcard, it will expand into zero arguments, i.e. to nothing.

This one works: If I have multiple wildcards, but at least one of them matches something, I get a warning, the non-matching ones become nothing, the matching one is expanded, and the command is executed:

fizzfaldt@buk ~/fishtest> command /bin/ls -1 -l b* c* d*
No matches for wildcard “c*”.
fish: command /bin/ls -1 -l b* c* d*
                               ^
No matches for wildcard “d*”.
fish: command /bin/ls -1 -l b* c* d*
                                  ^
-rw-rw-r-- 1 fizzfaldt fizzfaldt 0 Sep 16 00:38 bar
fizzfaldt@buk ~/fishtest> command /bin/ls -1 -l c* b* d*
No matches for wildcard “c*”.
fish: command /bin/ls -1 -l c* b* d*
                            ^
No matches for wildcard “d*”.
fish: command /bin/ls -1 -l c* b* d*
                                  ^
-rw-rw-r-- 1 fizzfaldt fizzfaldt 0 Sep 16 00:38 bar

Activity

  1. zanchey commented on Sep 16, 2015

    @zanchey
    Member

    See #1920, #1482.

  2. fizzfaldt commented on Sep 16, 2015

    @fizzfaldt
    Author

    At the very least, assuming the change in behavior is intentional, the documentation needs to be updated.
    For example, just deleting the line:

    If none of the wildcarded arguments sent to a command result in any matches, the command will not be executed.

    I do feel we have the worst of all worlds right now.
    We don't pass globs to subcommands, so we often have to escape them, and we have no control over how it works (no options to switch behavior).

    I liked getting errors when the globs failed, and would have preferred they always fail even if there were multiples. Is there no intention to have options then?

  3. ridiculousfish commented on Sep 16, 2015

    @ridiculousfish
    Member

    Today's "ls *.txt prints everything" behavior is horrible.

  4. faho commented on Sep 16, 2015

    @faho
    Member

    Yes, it is. But what's the correct behavior? The previous "ls *.txt fails, and you can't do anything to detect it or to suppress the error message" wasn't perfect either.

    For the ls case, you clearly don't want to have globs expand to nothing, but for for i in * or set foo * you probably want just that (and I believe we all agree that passing the bare "*" character is the worst possible solution).

  5. bgeron commented on Sep 22, 2015

    @bgeron

    I think the best option is to fail by default (so converts from sh don't hurt themselves), and add a emptyglob command mentioned in #1920 .

    Today's "ls *.txt prints everything" behavior is horrible.

    +1.

  6. ridiculousfish commented on Sep 27, 2015

    @ridiculousfish
    Member

    Another possibility is to make * and ** fail by default, but introduce new globs *? and **? that expand to empty instead of failing. I think I like that a bit better because it attaches to the glob instead of the command - what do others think?

  7. faho commented on Sep 27, 2015

    @faho
    Member

    introduce new globs ? and *? that expand to empty instead of failing

    First of all, we already have a "?" for globs. I presume you mean to ditch that and replace it with this?

    Otherwise, the idea of attaching it to globs sounds nice. I'm not sure if a more extensible syntax would make sense - zsh has glob qualifiers, which are very powerful but utterly unreadable. Unfortunately, I can't come up with a nice syntax.

  8. ridiculousfish commented on Sep 27, 2015

    @ridiculousfish
    Member

    ? matches any one character, while * matches zero or more characters. So *? has no real utility: any character matched by the ? could also be matched by the *. I propose grabbing this currently useless syntax to make a new glob *? that does the emptyglob thing.

    Ok, that's not really true: * matches the empty string, while *? only matches one-or-more characters. But one-or-more would still be available as ?* after this change.

  9. ridiculousfish commented on Sep 27, 2015

    @ridiculousfish
    Member

    I guess another, more conservative option is to make emptyglob a command instead of a command modifier. So instead of emptyglob ls *.txt it would be ls (emptyglob *.txt).

  10. faho commented on Sep 27, 2015

    @faho
    Member

    Ok, that's not really true: * matches the empty string, while ? only matches one-or-more characters. But one-or-more would still be available as ? after this change.

    And a one character glob that should come up empty instead of fail? "??" would either match any two characters or fail, or match any one character or come up empty - it's ambiguous.

    I guess another, more conservative option is to make emptyglob a command instead of a command modifier. So instead of emptyglob ls *.txt it would be ls (emptyglob *.txt).

    The issue I have with that is that I'd have to use it almost always.

    The best I could come up with is something like *[empty] - the brackets are already special for other substitutions and are currently not used for globs, so it makes sense to use them for something. It's also extensible - ?[empty,file] or *[0..5], but I'm not sure if it's fishy enough.

    Well, that or fail and make certain commands (set and for come to mind) special.

  11. anordal commented on Sep 27, 2015

    @anordal
    Contributor

    regex > emptyglob

  12. anordal commented on Sep 27, 2015

    @anordal
    Contributor

    I like how ? in *? and **? borrows syntax from regex. There it means, match once or nothing.

  13. ridiculousfish commented on Sep 27, 2015

    @ridiculousfish
    Member

    @faho can you elaborate on how you'd have to use it almost always?

  14. faho commented on Sep 27, 2015

    @faho
    Member

    I wrote a rather long and confused comment here before I recognized that I probably forgot that making globs fail also means not setting $status to 0. Currently globs behave a little confusing here - set and for fail with status 124, everything else (?) gets an empty argument and may therefore return 0 - meaning you can't detect if a glob with, say, ls failed.

    Frankly, all I'd need in that regard is that failing globs set status to non-zero (so that a simple or can work if you don't care why it failed) and a way to detect that the glob failed, not the command itself (a $globstatus variable?).

    So: Disregard, me thinkings ungood.

  15. added this to the milestone on Oct 16, 2015
  16. 35 remaining items

  17. bgeron commented on Feb 19, 2016

    @bgeron

    I think ridiculousfish meant 'rm -f', which is common in Makefiles. Make
    has nullglob behaviour if I'm not mistaken.

    Cheers, Bram

    On Fri, 19 Feb 2016, at 05:42 PM, anordal wrote:

    … but that's not success as far as rm is concerned:

    rm: missing operand
    Try 'rm --help' for more information.

    I mean that scripts have to check the number of arguments in order to
    not barf errors like that.

    — Reply to this email directly or view it on GitHub[1].

    Links:

    1. Failed wildcards still execute #2394 (comment)
  18. anordal commented on Feb 19, 2016

    @anordal
    Contributor

    Good point about rm -f! I'll count that as example number 1.

    If you mean targets in Makefiles, make picks the first target in the file if you don't specify any, so no nullglob behaviour wrt targets. But the wildcard function in Make has nullglob behaviour!

  19. bgeron commented on Feb 19, 2016

    @bgeron

    No, I meant rules.

    On Fri, 19 Feb 2016, at 06:10 PM, anordal wrote:

    Good point about rm -f! I'll count that as example number 1.

    If you mean targets in Makefiles, make picks the first target in the
    file if you don't specify any, so no.

    — Reply to this email directly or view it on GitHub[1].

    Links:

    1. Failed wildcards still execute #2394 (comment)
  20. ridiculousfish commented on Feb 19, 2016

    @ridiculousfish
    Member

    Yes, I should have typed rm -f, thanks

  21. faho commented on Feb 19, 2016

    @faho
    Member

    Specifically, for rm -f, it's often okay if one of the globs matches, and if none matches it might still count as a success.

  22. jplews commented on Mar 3, 2016

    @jplews

    This behaviour got me with cp where a source dir ended up empty and cp complained about not having a destination, in a situation with multiple sources that seems potentially dangerous, count seems to be the thing so all is well now.

    IMO makes sense to have glob --null and additional syntax for interactive efficiency, and re-purposing ? and adding ??, although * looks more like the symbol nullglob should have, with ? for failblob.

    Command substitution seems to cover everything, and would make more sense to a beginner. Having deliberately not used [ ] for my entire life I like the approach Fish is taking, and people should be pushed to using bash sub-processes like I often see with perl (even though bash has string stuff).

    I also think that the documentation is incorrect still, and perhaps it would be worth mentioning count there too.

  23. faho commented on Mar 3, 2016

    @faho
    Member

    I also think that the documentation is incorrect still, and perhaps it would be worth mentioning count there too.

    Count is mentioned in the documentation, unless you mean the ones hosted at fishshell.com, which are the ones from the last release.

  24. faho commented on May 19, 2016

    @faho
    Member

    There's not been new discussion in a while and I've not seen a huge need for syntactic sugar in practice (set works just fine), so I'm closing this for now. Maybe it'll come up again later.

  25. modified the milestones: , next-major on May 19, 2016
  26. added
    bug
    on May 19, 2016
  27. added a commit that references this issue on Apr 17, 2017
  28. locked as resolved and limited conversation to collaborators on Apr 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions