Test whether a glob has any matches in bash

后端 未结 19 2602
夕颜
夕颜 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:34

    Like this in bash (test files containing pattern):

    shopt -s nullglob
    compgen -W *pattern* &>/dev/null
    case $? in
        0) echo "only one file match" ;;
        1) echo "more than one file match" ;;
        2) echo "no file match" ;;
    esac
    

    It's far better than compgen -G: because we can discriminates more cases and more precisely.

    Can work with only one wildcard *

提交回复
热议问题