2

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

kingmilo
  • 10,274
Duby
  • 41

3 Answers3

2

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 $@
chronitis
  • 12,367
  • #! /bin/bash

    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
  • If I run it with./filename it runs, but I want to be able to run it with make all – Duby Nov 28 '12 at 13:02
0

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
orkoden
  • 189
0
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