Janik Vonrotz


2 min read

Simple task runner with bash/zsh scripts

When you work on multiple projects with different tech (Docker, npm, python, ..), a common interface to build, start, install or clean the state of the project is a powerful tool. There are various task runners for this job, however, every one of them requires you to install at least one dependency and so must everybody else who wants to use the project. What if we can use a task runner that is preinstalled on every computer? What about bash/zsh?

For a while I used Task as a task runner and created config files in the project root like this one:

taskfile.yml

version: '2'

tasks:
  up:
    cmds:
      - docker-compose up -d
  stop:
    cmds:
      - docker-compose stop
  run-script:
    cmds:
      - ./scripts/another-script

It is a nice solution for a common interface to manage projects. But as mentioned in the intro, that is not good enough. It requires a tool, which most developers are not familiar with. The solution for me was bash/zsh. I simply turned every task file into this:

task

#!/bin/zsh

set -euo pipefail

function help() {
echo
echo "$1 <command> [options]"
echo
echo "commands:"
echo
column -t -s"|" ./task.md | tail -n +3
echo
}

function start() {
    docker-compose up -d
}

function stop() {
    docker-compose stop
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    run-script)
        . ./scripts/script $2
        ;;
    *)
        echo task
        exit 1
esac

Create an alias alias task='./task', make the script executable chmod +x task and run task commands like this task start.

For instructions add a task.md file containing a markdown table with command instructions:

task.md

command|option|description
-|-|-
start| |Start docker container.
stop| |Stop docker container.
run-script|[option]|Run script with option.

Simple isn’t it?

Categories: Scripting
Tags: bash , task runner , zsh
Improve this page
Show statistic for this page