Checking if multiple directories exist

I need to create multiple directories if those directories do not exist already. How would you go by doing this.

What I have so far.

array=(one two three)

for I in ${array[@]}
do 
   if [[ ! -d ${I} ]]
   then
   mkdir ${I}
   fi
done

I have a good feeling this is done incorrectly. The error I am receiving is "syntax error near unexpected token 'fi'"

Try adding quotes around your variable. This worked for me without any errors:

array=(one two three)

for I in ${array[@]}
do
    if [ ! -d "$I" ]
    then
        mkdir -v "$I"
    fi
done

mkdir: created directory �one'
mkdir: created directory �two'
mkdir: created directory �three'

Do you even need an array? Or a loop? mkdir can be fed more than one thing at a time, can be told to skip dirs that already exist with -p, and the shell automatically splits on spaces without the help of an array. So, you can do this all in one op without arrays.

DIRS="a b c"

if ! mkdir -v -p $DIRS
then
        echo "Problem creating $DIRS"
        exit 1
fi