I need to list all the sub directories of a directory excluding those that match any of a list given as an argument: "SUBDIR1,SUBDIR2,...,SUBDIRN".
I came with the solution (from many sources) of using ls with a glob pattern. To test the concept I tried in the command line the following sequence which seems to work:
DIR="/path/to/dirs"
EXCLUDELIST="subdir1,subdir2"
#transform in a glob pattern for excluding given subdirectories
EXCLUDE="!(${EXCLUDELIST//,/|})"
LIST=$(cd $DIR && ls -l -d $EXCLUDE | grep -E "(^d)" | awk '{print $9}')
However, when I put it in a bash script unmodified I get this error
ls: cannot access !(subdir1|subdir2): No such file or directory
What am I doing wrong when putting this code in the script?