ksh - checking for filenames matching a pattern

ksh - checking for filenames matching a pattern

Post by Guenther Steinmet » Wed, 28 Aug 1996 04:00:00



I have the following problem: I want to check whether one or more
filenames matching a given pattern exist in a certain directory.
My solution (for checking for header files) is:

    #!/usr/bin/ksh
    # $1 is a directory
    integer found=0

    for hdr in $1/*.h* ; do
        if [[ $hdr != $1/\*.h\* ]]; then
            (( found = 1))
        fi
        break
    done

This looks rather awkward to me. There must be a more elegant and
simpler solution! Any ideas?

+----------------------------------------------------------------------+
|  Guenther Steinmetz           Mail:  Postfach 810247  81902 Muenchen |
|  Quality Consultant           Voice: (49) 89-9591-2892               |
|  Digital Equipment GmbH       Fax:   (49) 89-9591-2575               |
+----------------------------------------------------------------------+

 
 
 

ksh - checking for filenames matching a pattern

Post by Willy Wank » Sat, 31 Aug 1996 04:00:00




> I have the following problem: I want to check whether one or more
> filenames matching a given pattern exist in a certain directory.
> My solution (for checking for header files) is:

>     #!/usr/bin/ksh
>     # $1 is a directory
>     integer found=0

>     for hdr in $1/*.h* ; do
>         if [[ $hdr != $1/\*.h\* ]]; then
>             (( found = 1))
>         fi
>         break
>     done

Some form of the 'find' command should do the trick for you.  This works
in IRIX:

      result=$(find /usr/people/k/kives/News/* -prune -name '*.h*' -print)
      [[ -n result ]] && print FOUND SOME STUFF

Steve Kives

 
 
 

ksh - checking for filenames matching a pattern

Post by Ingo Pete » Sat, 31 Aug 1996 04:00:00


How about:

files=$(find $1 -name "*.h" -print)
found=${files:+TRUE}

if [[ $found = TRUE ]]; then
        ...

This approach will recursively check subdirectories too; if you don't want that you need to read up on the -prune option for "find".

Hope this helps,
Ingo

P.S.  This approach is probably somewhat slower than your code, especially when there are lots of header files present.

 
 
 

1. ksh pattern matching when pattern is in a variable

Witness this:


  [[ work = $exclude ]] && print excluded

The print is executed on AIX 433, but not on AIX 5.1 or SunOS 5.9.

Looking at the original AT&T ksh book by David Korn, on pg 156 under
Conditional Commands, in the [[ ... ]] section, it says

"ksh expands the operands(s) for each conditional expression primitive
for command substitution, parameter expansion, and quote removal as
required to evaluate the command."

To me, this means it should be expanding $exclude before it interprets
it as a pattern.  And this is certainaly The Right Thing To Do.  What
am I missing?

John.

2. Weird modem(s) problem/ VMWare

3. Pattern matching and extracting the data which matches the pattern

4. Aopen O-35G mouse.

5. KSH Pattern Match Deletions

6. Help w/ second HD

7. ksh pattern matching

8. I don't need to mess it (automatic SSH)

9. ksh conditional (string match pattern)

10. ksh discrepancy on pattern matching

11. ksh and pattern matching

12. How can I pattern-match in ksh?

13. my fault about pattern matching in ksh