0

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)...

2 Answers2

1

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

if [[ -d $i -a $i =~ ^[0-9]+$ ]] ; then
  ...
fi
Ignacio Vazquez-Abrams
  • 45,939
  • 6
  • 79
  • 84
0

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 
Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151