help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] The best way to test whether a directory has *.txt files


From: Steven W. Orr
Subject: Re: [Help-bash] The best way to test whether a directory has *.txt files?
Date: Thu, 28 Jun 2012 09:31:27 -0400
User-agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.28) Gecko/20120306 Thunderbird/3.1.20

On 6/28/2012 8:00 AM, Greg Wooledge wrote:
On Thu, Jun 28, 2012 at 04:44:49PM +0800, Clark WANG wrote:
There can be more than one way to test whether a directory contains
*.txt files (to be used with 'find -exec'). But I'm wondering what is
the best way (in terms of convenience to type) to do it.

There is no "best way".  There are ways that don't work, and there are
ways that STILL don't work, and then, every once in a great while, you
may find a way that actually does work.

For example, any trick that expands a glob and then compares the result
to the literal glob is wrong, because the glob itself is also a valid
filename.

# Wrong.
touch '*'
if [[ `echo *` = '*' ]]; then echo "Derp, I think there are no files"; fi
# Wrong.

Does anybody have any tricks on this?

http://mywiki.wooledge.org/BashFAQ/004

I've ever seen this:

( shopt -s nullglob; set -- /the/dir/*.txt; (( $# )) )

I'm not fond of subshells for this kind of task, due to the performance
penalties.  Personally I'd just use:

shopt -s nullglob dotglob
files=(/the/dir/*.txt); address@hidden
shopt -u nullglob dotglob

This also has the advantage of leaving an array populated with the
filenames for later iteration or whatever.  With the subshell approach,
that array is discarded.


Greg is right on the money. Parens mean sub-shells. They do not mean prioritization like they do in other languages.

Here's a snippet of how I reliably do what you want. This is not a minimal example. It's just something I have lying around. The trick is to set nullglob and to then restore it. The glob is assigned to an array so I can see how many matches I got. Zero is not enough and more than 1 is too many.

    DIR=${DIST}${ARCH_STR}
    # set nullglob so the we get no expansion if the glob fails.
    # Assign to an array so the expansion can be done inline.
    shopt -s nullglob
    # It can be either a 2, a 4 or a 10.
PKG=( ${DIR}/${RPM_BASE_NAME}${KERN_VAR}-{2,4,10}.*_${RPM_DISTRO}-[1-9].${RPM_ARCH_STR}.rpm )
    shopt -u nullglob
    # Check for whether the expansion failed.
    if [[ address@hidden -eq 0 ]]
    then
        echo "ERROR:rpm file not found."
        exit 1
    fi
    # Check if we got too many matches.
    if [[ address@hidden -gt 1 ]]
    then
        echo "INTERNAL ERROR: Too many rpm files found."
        echo "Matching packages: address@hidden"
        exit 1
    fi

I have other code that does similar things that are more general, but this works as the the problem is described.


--
Time flies like the wind. Fruit flies like a banana. Stranger things have  .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net



reply via email to

[Prev in Thread] Current Thread [Next in Thread]