Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm not sure exactly how the code should be written but I want to test a file/folder for naming patterns, something like:

if [ -d $i ] && [ regex([0-9].,$i) {
       do something
    }

I want it to check if the file/folder is a directory and that the name of it is a number (i.e. 1 or 101 or 10007)...

share|improve this question
add comment

2 Answers

If you're using Bash earlier than version 3.2, the regex match operator =~ either doesn't exist, works differently or has bugs. You can use this glob expansion form in that case (or if you're using a POSIX shell that doesn't have it).

case $i in
    *[^0-9]*)
        do_not_a_number_stuff
        ;;
     *)
        if [ -d "$i" ]
        then
            do_something
        fi
        ;;
 esac 
share|improve this answer
add comment

[ cannot do regular expressions. However, [[ can:

if [[ -d $i -a $i =~ ^[0-9]+$ ]] ; then
  ...
fi
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.