Home > front end >  If not exists, create a folder - Error in makefile
If not exists, create a folder - Error in makefile

Time:07-04

I searched over the internet for a command to create a folder if it doesn't exist. I found it and put in my makefile

.DEFAULT_GOAL := all
folder := "myfolder"

createfolder:
    [ ! -d ${folder} ] && mkdir -p ${folder}

nextstep:
    echo "Got here!"

all: createfolder nextstep

When the folder doesn't exist it's created correctly. But I get an error if the folder already exists.

$ ls
makefile
$ make
[ ! -d "myfolder" ] && mkdir -p "myfolder"
echo "Got here!"
Got here!
$ ls
makefile  myfolder
$ make
[ ! -d "myfolder" ] && mkdir -p "myfolder"
make: *** [makefile:5: createfolder] Error 1

I don't get why the command would give an error if the condition [ ! -d "myfolder" ] is before the mkdir and it shouldn't even execute the second command.

How can I solve it?

CodePudding user response:

You can use if then fi syntax

.DEFAULT_GOAL := all
folder := "myfolder"

createfolder:
        if [ ! -d ${folder} ]; then mkdir -p ${folder}; fi

nextstep:
        echo "Got here!"

all: createfolder nextstep

CodePudding user response:

this is what make is for.

${folder}: 
        mkdir -p ${folder}
  • Related