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
Brendan [ Fr, 09 November 2007 22:11 ] [ ID #1866349 ]

Re: bash - test for no files matching *.foo

On Nov 9, 4:11 pm, Brendan <brendandetra... [at] 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 ))
OldSchool [ Fr, 09 November 2007 22:33 ] [ ID #1866350 ]

Re: bash - test for no files matching *.foo

Brendan <brendandetracey [at] 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
Kenan Kalajdzic [ Fr, 09 November 2007 22:38 ] [ ID #1866351 ]

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
cfajohnson [ Sa, 10 November 2007 09:18 ] [ ID #1867251 ]

Re: bash - test for no files matching *.foo

On Nov 10, 4:18 am, "Chris F.A. Johnson" <cfajohn... [at] 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?
Brendan [ So, 11 November 2007 01:35 ] [ ID #1867708 ]

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... [at] 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
cfajohnson [ So, 11 November 2007 02:06 ] [ ID #1867709 ]

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 help. I'll have to get more familiar with the set
command.
Brendan [ So, 11 November 2007 21:36 ] [ ID #1867712 ]
Linux » comp.unix.shell » bash - test for no files matching *.foo

Vorheriges Thema: Top 10 posters comp.unix.shell
Nächstes Thema: Transfert file with scp (ssh)