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 > bash - test for no files matching *.foo
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

bash - test for no files matching *.foo

Réponse
 
LinkBack Outils de la discussion
Vieux 09/11/2007, 21h11   #1
Brendan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut bash - test for no files matching *.foo

Please bear with me onthis stupid question.
I have a bash script with a line:

<code>
w3flag=$(( $(ls *.ps | wc -w) == 18 ))
</code>

The problem is when there are no files matching the pattern *.ps the
code works but prints out an error message.

How can I either:
1) suppress the error message for this line
or
2) short circuit this test if there are no files matching *.ps

Thanks

  Réponse avec citation
Vieux 09/11/2007, 21h33   #2
OldSchool
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bash - test for no files matching *.foo

On Nov 9, 4:11 pm, Brendan <brendandetra...@yahoo.com> wrote:
> Please bear with me onthis stupid question.
> I have a bash script with a line:
>
> <code>
> w3flag=$(( $(ls *.ps | wc -w) == 18 ))
> </code>


w3flag=$(( $(ls *.ps 2>/dev/null | wc -w) == 18 ))

  Réponse avec citation
Vieux 09/11/2007, 21h38   #3
Kenan Kalajdzic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bash - test for no files matching *.foo

Brendan <brendandetracey@yahoo.com> wrote:
> Please bear with me onthis stupid question.
> I have a bash script with a line:
>
> <code>
> w3flag=$(( $(ls *.ps | wc -w) == 18 ))
> </code>
>
> The problem is when there are no files matching the pattern *.ps the
> code works but prints out an error message.
>
> How can I either:
> 1) suppress the error message for this line
> or
> 2) short circuit this test if there are no files matching *.ps


A simple solution is to redirect standard error of ls to /dev/null:

w3flag=$(( $(ls *.ps 2>/dev/null | wc -w) == 1 ))

--
Kenan Kalajdzic
  Réponse avec citation
Vieux 10/11/2007, 08h18   #4
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bash - test for no files matching *.foo

On 2007-11-09, Brendan wrote:
>
> Please bear with me onthis stupid question.
> I have a bash script with a line:
>
><code>
> w3flag=$(( $(ls *.ps | wc -w) == 18 ))
></code>
>
> The problem is when there are no files matching the pattern *.ps the
> code works but prints out an error message.
>
> How can I either:
> 1) suppress the error message for this line
> or
> 2) short circuit this test if there are no files matching *.ps


You don't need either ls or wc:

set -- *.ps
if [ -f "$1" ]
then
w3flag=$#
else
w3flag=0
fi

Special cases may need a slightly more elaborate test.

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
  Réponse avec citation
Vieux 11/11/2007, 00h35   #5
Brendan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bash - test for no files matching *.foo

On Nov 10, 4:18 am, "Chris F.A. Johnson" <cfajohn...@gmail.com> wrote:
> On 2007-11-09, Brendan wrote:
>
> > Please bear with me onthis stupid question.
> > I have a bash script with a line:

>
> ><code>
> > w3flag=$(( $(ls *.ps | wc -w) == 18 ))
> ></code>

>
> > The problem is when there are no files matching the pattern *.ps the
> > code works but prints out an error message.

>
> > How can I either:
> > 1) suppress the error message for this line
> > or
> > 2) short circuit this test if there are no files matching *.ps

>
> You don't need either ls or wc:
>
> set -- *.ps
> if [ -f "$1" ]
> then
> w3flag=$#
> else
> w3flag=0
> fi
>
> Special cases may need a slightly more elaborate test.
>
> --
> Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
> Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
> ===== My code in this post, if any, assumes the POSIX locale
> ===== and is released under the GNU General Public Licence- Hide quoted text -
>
> - Show quoted text -


I do not understand this one. The output will be either 0 or the
number of files matching the pattern? And why does the $1 not refer to
the first argument passed into the script, but instead to the argument
of the set command?

  Réponse avec citation
Vieux 11/11/2007, 01h06   #6
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bash - test for no files matching *.foo

On 2007-11-11, Brendan wrote:
>
> On Nov 10, 4:18 am, "Chris F.A. Johnson" <cfajohn...@gmail.com> wrote:
>> On 2007-11-09, Brendan wrote:
>>
>> > Please bear with me onthis stupid question.
>> > I have a bash script with a line:

>>
>> ><code>
>> > w3flag=$(( $(ls *.ps | wc -w) == 18 ))
>> ></code>

>>
>> > The problem is when there are no files matching the pattern *.ps the
>> > code works but prints out an error message.

>>
>> > How can I either:
>> > 1) suppress the error message for this line
>> > or
>> > 2) short circuit this test if there are no files matching *.ps

>>
>> You don't need either ls or wc:
>>
>> set -- *.ps
>> if [ -f "$1" ]
>> then
>> w3flag=$#
>> else
>> w3flag=0
>> fi
>>
>> Special cases may need a slightly more elaborate test.

>
> I do not understand this one. The output will be either 0 or the
> number of files matching the pattern?


There is no output in that snippet. The number of files matching
the pattern will be stored in $w3flag.

> And why does the $1 not refer to the first argument passed into the
> script, but instead to the argument of the set command?


Once any options are used up, the remaining arguments to set
replace the positional parameters. If you want to use the previous
positional parameters, save them into meaningful variables before
using set.

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
  Réponse avec citation
Vieux 11/11/2007, 20h36   #7
Brendan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bash - test for no files matching *.foo

> > I do not understand this one. The output will be either 0 or the
> > number of files matching the pattern?

>
> There is no output in that snippet. The number of files matching
> the pattern will be stored in $w3flag.
>


That's what I meant by "output".
Thanks for the . I'll have to get more familiar with the set
command.

  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 07h26.


É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,15879 seconds with 15 queries