The basics

November 17, 2021 - Reading time: 2 minutes

Good documentation

Run commands when files or Makefile changes

Useful to run commands that generate documentation.

# Declare a variable that includes all *.sh files in the folder, and the Makefile
FILES = $(shell find . -name '*.sh') Makefile

# When no targets are added, it runs help 
all: help

# PHONY target means that it won't create a file, but it's just used as a name
# The "@" before the command means that the command won't be printed. 
# It looks for "##" and prints it as help  
.PHONY: help
help:
    @sed -n 's/^##//p' Makefile

## first: generate some files if .sh or Makefile changes 
# Run if any of the *.sh files were modified, or Makefile. 
first: $(FILES)
    echo "the var is: $(FILES)"
    touch first

## clean: delete generated files 
.PHONY: clean 
clean:
    rm -rf first 

To show the help:

test@localhost: make
 first: generate some files if .sh or Makefile changes 
 clean: delete generated files 
test@localhost: make help
 first: generate some files if .sh or Makefile changes 
 clean: delete generated files 

Create files:

test@localhost: make first
echo "the var is: ./c.sh ./a.sh ./b.sh Makefile"
the var is: ./c.sh ./a.sh ./b.sh Makefile
touch first
test@localhost: make first
make: 'first' is up to date.

Edit a shell script, then make again:

test@localhost: touch a.sh 
test@localhost: make first
echo "the var is: ./c.sh ./a.sh ./b.sh Makefile"
the var is: ./c.sh ./a.sh ./b.sh Makefile
touch first

Clean and make again:

test@localhost: make clean 
rm -rf first 
test@localhost: make first
echo "the var is: ./c.sh ./a.sh ./b.sh Makefile"
the var is: ./c.sh ./a.sh ./b.sh Makefile
touch first