In his answer @Grundlefleck explains how to check whether a directory exists or not. I tried some to use this inside a makefile
as follow:
foo.bak: foo.bar echo "foo" if [ -d "~/Dropbox" ]; then echo "Dir exists" fi
Running make foo.bak
(given that foo.bar
exists) yields the following error:
echo "foo" foo if [ -d "~/Dropbox" ]; then /bin/sh: -c: line 1: syntax error: unexpected end of file make: *** [foo.bak] Error 2
The workaround I made was to have a standalone bash script where the test is implemented and I called the script from the makefile
. This, however, sounds very cumbersome. Is there a nicer way to check whether a directory exists from within a makefile
?
just do @rm -f myfile. Because of the "-f" flag, "rm" will exit with 0 regardless of whether the file exists or not. Or, -rm myfile the lead dash telling make to ignore any error.
Yes, a Makefile can have a directory as target. Your problem could be that the cd doesn't do what you want: it does cd and the git clone is carried out in the original directory (the one you cd ed from, not the one you cd ed to). This is because for every command in the Makefile an extra shell is created.
In order to check if a directory exists in Bash using shorter forms, specify the “-d” option in brackets and append the command that you want to run if it succeeds. [[ -d <directory> ]] && echo "This directory exists!" [ -d <directory> ] && echo "This directory exists!"
Make commands, if a shell command, must be in one line, or be on multiple lines using a backslash for line extension. So, this approach will work:
foo.bak: foo.bar echo "foo" if [ -d "~/Dropbox" ]; then echo "Dir exists"; fi
Or
foo.bak: foo.bar echo "foo" if [ -d "~/Dropbox" ]; then \ echo "Dir exists"; \ fi
This approach functions with minimal echos:
.PHONY: all all: ifneq ($(wildcard ~/Dropbox/.*),) @echo "Found ~/Dropbox." else @echo "Did not find ~/Dropbox." endif
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With