I have a shell script, and would like to convert it to a makefile so that it can be executed with the command 'make all'.
How would I go about this?
Thanks
You can create a minimal makefile containing:
all:
YOUR SHELL COMMANDS HERE
But you haven't really gained anything by doing so unless you rewrite it to make use of some of the features of a Makefile - multiple targets, target dependencies, etc. See here (or many other Makefile tutorials) for some information about what you can do with a makefile.
(Note that the shell commands are executed with sh
separately for each line, so if you're doing anything complex like loops, setting variables it won't just work, and the indentation of commands needs to be a TAB character, not spaces.)
A quick Makefile for the case described (normal warnings, probably has all sorts of edge cases, might kill your dog, etc):
src=$(wildcard pictures/*.jpg)
out=$(subst pictures,thumb,$(src))
all : $(out)
thumb/%.jpg: pictures/%.jpg
convert $< -thumbnail 100 $@
It's best if you separate your shell scripts out into several parts. E.g. prebuild, build, postbuild. Several small tasks make it easier to test the script.
clean:
rm -rf build
build:
gcc sample.c
changelog:
git log >> changelog.txt
package: changelog build
zip release.zip a.out changelog.txt
all: clean package
clean:
@-rm -rf build
@echo removed
build:
gcc sample.c
changelog:
git log >> changelog.txt
package: changelog build
zip release.zip a.out changelog.txt
all: clean package
cd pictures for i in *.jpg; do if [ "$i" -nt "../thumbs/$i" ]; then convert "$i" -thumbnail 100 "../thumbs/$i"; fi done; That's the shell script. What can do so that the script can be called with 'make all'. Thank you very much for your help
– Duby Nov 28 '12 at 12:49