|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Is there an idiom that folds the no-files case into the files-exist case so
they can be handled with a single mechanism? $ bash --version GNU bash, version 3.1.17(1)-release (i486-pc-linux-gnu) Copyright (C) 2005 Free Software Foundation, Inc. $ ls $ chmod a-x * chmod: cannot access `*': No such file or directory $ I could do this $ chmod a-x * 2>/dev/null $ but that just hides the error; there are still two cases (the exit status tells the tale). I could do this $ chmod a-x * 2>/dev/null ; : $ but that's more hiding. This works $ files=$(find . -type f) $ [ -z "$files" ] || chmod a-x $files $ and is, I suppose, good documentation, but it's too much machinery. This problem is similar to handling zero-iteration loops in programs, but it's irratating that $ for i in * ; do chmod a-x $i ; done chmod: cannot access `*': No such file or directory $ doesn't work. Anything else? |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
R. Clayton wrote:
> Is there an idiom that folds the no-files case into the files-exist case so > they can be handled with a single mechanism? > > $ bash --version > GNU bash, version 3.1.17(1)-release (i486-pc-linux-gnu) > Copyright (C) 2005 Free Software Foundation, Inc. > > $ ls > > $ chmod a-x * > chmod: cannot access `*': No such file or directory > > $ > > I could do this > > $ chmod a-x * 2>/dev/null > > $ > > but that just hides the error; there are still two cases (the exit status tells > the tale). I could do this > > $ chmod a-x * 2>/dev/null ; : > > $ > > but that's more hiding. The "problem" in your example is that chmod(1) requires files to operate on; a simple chmod a-x without arguments won't work, even if the shell file globbing mechanism would produce an empty argument list instead of taking the * literally. > This works > > $ files=$(find . -type f) > > $ [ -z "$files" ] || chmod a-x $files > > $ You could do set -- * [ -f "$1" ] but I am not sure whether you like that since it does not avoid the test operation that you seem to dislike. > and is, I suppose, good documentation, but it's too much machinery. This > problem is similar to handling zero-iteration loops in programs, but it's > irratating that > > $ for i in * ; do chmod a-x $i ; done > chmod: cannot access `*': No such file or directory > > $ > > doesn't work. Anything else? A while/read loop find . -type f | while read -r f ; do chmod a-x "$f" ; done Janis |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
R. Clayton wrote:
> $ chmod a-x * > chmod: cannot access `*': No such file or directory As Janis said, the error is due to chmod /always/ expecting an argument. However, you might find the nullglob feature of bash useful sometimes. $ ls $ echo * $ * $ shopt -s nullglob $ echo * $ Note, however, that this does not solve the problem for all those situations where an argument is expected anyway, like eg chmod: $ chmod a-x * chmod: missing operand after `a-x' Try `chmod --' for more information. This is something that cannot really be avoided, AFAIK. -- All the commands are tested with bash and GNU tools, so they may use nonstandard features. I try to mention when something is nonstandard (if I'm aware of that), but I may miss something. Corrections are welcome. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
The "problem" in your example is that chmod(1) requires files to operate on;
a simple chmod a-x without arguments won't work, even if the shell file globbing mechanism would produce an empty argument list instead of taking the * literally. True, but I was whining more about misbehaving glue code. Here's another irritating one what doesn't work: $ find . -type f | xargs chmod a-x chmod: missing operand after `a-x' Try `chmod --' for more information. $ although there is a gnu extension that makes it behave: $ find . -type f | xargs --no-run-if-empty chmod a-x $ |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
R. Clayton wrote:
> The "problem" in your example is that chmod(1) requires files to operate on; > a simple chmod a-x without arguments won't work, even if the shell file > globbing mechanism would produce an empty argument list instead of taking the > * literally. > > True, but I was whining more about misbehaving glue code. > Here's another irritating one what doesn't work: Yes, but actually it's the same case as upthread; if the list provided by find is empty chmod won't get the arguments it expects. Janis > > $ find . -type f | xargs chmod a-x > chmod: missing operand after `a-x' > Try `chmod --' for more information. > > $ > > although there is a gnu extension that makes it behave: > > $ find . -type f | xargs --no-run-if-empty chmod a-x > > $ |
|
![]() |
| Outils de la discussion | |
|
|