bshchk.web/content/_index.md

1.8 KiB

+++ +++

why?

lets take this script:

rm -fr data.json
curl https://url.com/data.json -o data.json

the problem here is that curl might not be installed on second line, but the data is already deleted.

it will result in data.json being irreparably deleted and no way to reinstall it

solution

so generally, you would want to check if the dependencies of your bash script are installed, right?

you could write up something like this and put it in the start:

if ! command -v curl; then
    echo no curl!
    exit 1
fi

but that's just not reliable and barely maintainable. so that's why i made this thing. it parses your script, and then appends a small snippet at the earliest place in your program that would check if all deps are installed.

you can use it like this:

#!/usr/bin/env bash
curl https://url.com

then compile it like that: bshchk program.sh dist.sh.

also you can read the script from stdin: cat program.sh | bshchk - dist.sh, or get the output to stdout: bshchk program.sh > dist.sh

it would output something like this:


#!/usr/bin/env bash

# This is the runtime dependency checker
# Please do not remove the following lines.
deps=('curl')
non_ok=()

for d in $deps
do
    if ! command -v $d > /dev/null 2>&1; then
        non_ok+=$d
    fi
done

if (( ${#non_ok[@]} != 0 )); then
    >&2 echo "RDC Failed!"
    >&2 echo "  This program requires these commands:"
    >&2 echo "  > $deps"
    >&2 echo "    --- "
    >&2 echo "  From which, these are missing:"
    >&2 echo "  > $non_ok"
    >&2 echo "Make sure that those are installed and are present in \$PATH."
    exit 1
fi

unset non_ok
unset deps
# Dependencies are OK at this point

curl https://url.com

website's source code available here