bash - test for no files matching *.foo
bash - test for no files matching *.foo
am 09.11.2007 22:11:05 von Brendan
Please bear with me onthis stupid question.
I have a bash script with a line:
w3flag=$(( $(ls *.ps | wc -w) == 18 ))
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
Re: bash - test for no files matching *.foo
am 09.11.2007 22:33:03 von OldSchool
On Nov 9, 4:11 pm, Brendan wrote:
> Please bear with me onthis stupid question.
> I have a bash script with a line:
>
>
> w3flag=$(( $(ls *.ps | wc -w) == 18 ))
>
w3flag=$(( $(ls *.ps 2>/dev/null | wc -w) == 18 ))
Re: bash - test for no files matching *.foo
am 09.11.2007 22:38:55 von Kenan Kalajdzic
Brendan wrote:
> Please bear with me onthis stupid question.
> I have a bash script with a line:
>
>
> w3flag=$(( $(ls *.ps | wc -w) == 18 ))
>
>
> 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
Re: bash - test for no files matching *.foo
am 10.11.2007 09:18:10 von cfajohnson
On 2007-11-09, Brendan wrote:
>
> Please bear with me onthis stupid question.
> I have a bash script with a line:
>
>
> w3flag=$(( $(ls *.ps | wc -w) == 18 ))
>
>
> 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
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
Re: bash - test for no files matching *.foo
am 11.11.2007 01:35:30 von Brendan
On Nov 10, 4:18 am, "Chris F.A. Johnson" wrote:
> On 2007-11-09, Brendan wrote:
>
> > Please bear with me onthis stupid question.
> > I have a bash script with a line:
>
> >
> > w3flag=$(( $(ls *.ps | wc -w) == 18 ))
> >
>
> > 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
> 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?
Re: bash - test for no files matching *.foo
am 11.11.2007 02:06:41 von cfajohnson
On 2007-11-11, Brendan wrote:
>
> On Nov 10, 4:18 am, "Chris F.A. Johnson" wrote:
>> On 2007-11-09, Brendan wrote:
>>
>> > Please bear with me onthis stupid question.
>> > I have a bash script with a line:
>>
>> >
>> > w3flag=$(( $(ls *.ps | wc -w) == 18 ))
>> >
>>
>> > 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
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
Re: bash - test for no files matching *.foo
am 11.11.2007 21:36:26 von Brendan
> > 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.