[plug] Bash help

James Devenish devenish at guild.uwa.edu.au
Sun Dec 28 18:21:31 WST 2003


Just seeking some assistance from the Bash (GNU Bourne-Again SHell)
experts. If anyone knows the canonical Bash equivalents for the
following constructs, please let me know. The aim is to exclude a small
number of subdirectories from globbing in a directory that contains
hundreds of files and subdirectories. Basically, subdirectories must
matching an initial pattern, but exclusion should occur if the
subdirectory name matches a second pattern. I have shown some examples,
below. You may imagine that 'positive_pattern' is 'test_*' while
'negative_pattern' is '*_b' (e.g. to match subdirectories like test_a
and test_c but exclude test_b).

1/ Zsh-style globbing demonstration (what I would normally do)

# Choosing directories
for subdir in positive_pattern~negative_pattern(/); do
    echo Matched $subdir
done

# Choosing files
for file in (positive_pattern~negative_pattern)/filename_pattern; do
    echo Matched $file
done

2/ Equivalent non-globbing demonstration (for verbosity)

# Choosing directories
for entry in positive_pattern; do
    if [[ -d $entry && $entry != negative_pattern ]]; then
        echo Subdir $entry was not excluded
    fi
done

# Choosing files
for entry in positive_pattern/filename_pattern; do
    if [[ $entry(:h) != negative_pattern && -d $entry(:h) ]]; then
        echo File $entry was not excluded
    fi
done

3/ Portable demonstration

# Choosing directories
for entry in positive_pattern; do
    if [ -d $entry -a $entry != negative_pattern ]; then
        echo Subdir $entry was not excluded
    fi
done

# Choosing files
for entry in positive_pattern/filename_pattern; do
    subdir=$(dirname $entry)
    if [ $subdir != negative_pattern -a -d $subdir ]; then
        echo File $entry was not excluded
    fi
done

PS. From looking at the Bash info pages, I thought perhaps `shopt -s
extglob' followed by positive_pattern!(negative_pattern) or
positive_pattern!(negative_pattern)/filename_pattern might work, but
this strategy failed (what am I overlooking?).





More information about the plug mailing list