PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > Unifying files and no files.
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Unifying files and no files.

Réponse
 
LinkBack Outils de la discussion
Vieux 21/03/2008, 04h33   #1
R. Clayton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Unifying files and no files.

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?
  Réponse avec citation
Vieux 21/03/2008, 05h51   #2
Janis Papanagnou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Unifying files and no files.

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
  Réponse avec citation
Vieux 21/03/2008, 09h49   #3
pk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Unifying files and no files.

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.
  Réponse avec citation
Vieux 22/03/2008, 01h20   #4
R. Clayton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Unifying files and no files.

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

$
  Réponse avec citation
Vieux 22/03/2008, 01h38   #5
Janis Papanagnou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Unifying files and no files.

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
>
> $

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 15h20.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15984 seconds with 13 queries