Hacker News new | ask | show | jobs
by GordonS 2385 days ago
Is there a way to easily have something like make targets in a shell script, without a ton of boilerplate?

> Make syntax is really odd

I don't find it particularly strange, except my biggest peeve - the insistence on tabs!

1 comments

Here's a named task runner shellscript (as compared to a Makefile full of .PHONY being used as a named task runner).

    #!/bin/sh
    set -e

    case "$1" in
        build)
            ;;
        run)
            ;;
        clean)
            ;;
        *)
            echo "unknown: $1"; exit 2
            ;;
    esac
Someone else ITT hinted it could be done like the following. But the last line is dubious because it will happily run anything on PATH.

    #!/bin/sh
    set -e

    test $# -gt 0

    build() {
        :
    }

    run() {
        :
    }

    clean() {
        :
    }

    "$@"
> (as compared to a Makefile full of .PHONY being used as a named task runner)

Personally, I've never used PHONY, or had a need to.

That said, your bash examples are pretty simple - I especially like the 2nd example, as it would trivially allow having targets that ran other targets, e.g. an "all" target from a makefile:

``` all: build push

build: @docker build --tag ${IMG} --tag ${UNSTABLE} .

rebuild: @docker build --no-cache --tag ${IMG} --tag ${UNSTABLE} .

push: @docker push ${NAME} ```