scary cool bash scripting inside a Makefile

Makefiles are both scary and wonderful. When both these adjectives are involved, it often makes for interesting hacking. This is likely the reason I use bash.

In any case, I digress, back to real work. I use Makefiles as a general purpose tool to launch any of a number of shell scripts which I use to maintain my code, and instead of actually having external shell scripts, I just build any necessary bash right into the Makefile.

One benefit of all this is that when you type “Make <target>”, the <target> can actually autocomplete which makes your shell experience that much more friendly.

In any case, let me show you the code in question. Please note the double $$ for shell execution and for variable referencing. The calls to rsync and sort make me pleased.

rsync -avz --include=*$(EXT) --exclude='*' --delete dist/ $(WWW)
# empty the file
echo -n '' > $(METADATA)
cd $(WWW); 
for i in *$(EXT); do 
b=$$(basename $$i $(EXT)); 
V=$$(echo -n $$(basename "`echo -n "$$b" | rev`" 
"`echo -n "$(NAME)-" | rev`") | rev); 
echo $(NAME) $$V $$i >> $(METADATA); 
sort -V -k 2 -o $(METADATA) $(METADATA)		# sort by version key

The full Makefile can be found inside of the bash-tutor tarball.


Nothing yet.

Post a comment

(sorry but the spammers were getting too crazy!)

Thank you

Your comment has been submitted and will be published if it gets approved.

Click here to see the patch you generated.