Test whether a glob has any matches in bash

后端 未结 19 2348
夕颜
夕颜 2020-11-22 15:51

If I want to check for the existence of a single file, I can test for it using test -e filename or [ -e filename ].

Supposing I have a glob

19条回答
  •  情歌与酒
    2020-11-22 16:37

    test -e has the unfortunate caveat that it considers broken symbolic links to not exist. So you may want to check for those, too.

    function globexists {
      test -e "$1" -o -L "$1"
    }
    
    if globexists glob*; then
        echo found
    else
        echo not found
    fi
    

提交回复
热议问题