Differences between revisions 13 and 14
Revision 13 as of 2008-10-06 00:45:44
Size: 2343
Editor: gw
Comment: backticks to $()
Revision 14 as of 2008-10-06 03:19:49
Size: 2230
Editor: GreyCat
Comment: No, Bourne shell doesn't have $(). Or [.
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
 ''I just deleted three completely '''wrong''' answers from this question. Please, people, make sure that when you add to the FAQ, your answers''
  * answer the question that was asked, and
  * actually '''work'''
 ''Thanks. -- GreyCat''
Line 8: Line 4:
In BASH, you can do this safely and easily with the nullglob and dotglob options (which change the behaviour of [:glob:globbing]), and arrays: In Bash, you can do this safely and easily with the nullglob and dotglob options (which change the behaviour of [:glob:globbing]), and arrays:
Line 11: Line 7:
    # Bash
Line 20: Line 17:
    # Bash
Line 30: Line 28:
    # Bash
Line 41: Line 40:
   # Bourne
Line 49: Line 49:
    cd foo
    if [ "$(printf '%s %s %s' .* *)" = '. .. *' ] && [ ! -f '*' ]
    # Bourne
    # (Of course, the system must have printf(1).)
cd foo || exit 1
    if test "`printf '%s %s %s' .* *`" = '. .. *' && ! test -f '*'
Line 56: Line 58:
Yes, it's extremely ugly, but it should be more portable than anything depending on ls output. Even ls -A solutions can break (HPUX for one, if you are root). Yes, it's extremely ugly, but it should be more portable than anything depending on `ls` output. Even `ls -A` solutions can break (HPUX for one, if you are root).

Anchor(faq4)

How can I check whether a directory is empty or not? How do I check for any *.mpg files?

In Bash, you can do this safely and easily with the nullglob and dotglob options (which change the behaviour of [:glob:globbing]), and arrays:

    # Bash
    shopt -s nullglob dotglob
    files=(*)
    (( ${#files[*]} )) || echo directory is empty
    shopt -u nullglob dotglob

As you can see we unset the nullglob after using it, to prevent it affecting other globs in the script in unexpected ways. nullglob also simplifies various other operations:

    # Bash
    shopt -s nullglob
    for i in *.zip; do
        blah blah "$i"  # No need to check $i is a file.
    done
    shopt -u nullglob

Without the nullglob, that would have to be:

    # Bash
    for i in *.zip; do
        [[ -f $i ]] || continue  # If no .zip files, i becomes *.zip
        blah blah "$i"
    done

(You may want to use the latter anyway, if there's a possibility that the glob may match directories in addition to files.)

In fact, you may wish to avoid the direct question altogether. Usually people want to know whether a directory is empty... because they want to do something involving the files therein, etc. Look to the larger question. For example, one of these [:UsingFind:find-based examples] may be an appropriate solution:

   # Bourne
   find "$somedir" -type f -exec echo Found unexpected file {} \;
   find "$somedir" -maxdepth 0 -empty -exec echo {} is empty. \;  # GNU/BSD
   find "$somedir" -type d -empty -exec cp /my/configfile {} \;   # GNU/BSD

If your script needs to run with various shell implementations, you can try using an external program like python, perl, or find as indicated above, or you can try something like:

    # Bourne
    # (Of course, the system must have printf(1).)
    cd foo || exit 1
    if test "`printf '%s %s %s' .* *`" = '. .. *' && ! test -f '*'
    then
        echo "directory is empty"
    fi

Yes, it's extremely ugly, but it should be more portable than anything depending on ls output. Even ls -A solutions can break (HPUX for one, if you are root).

BashFAQ/004 (last edited 2023-03-28 07:52:15 by emanuele6)