bshchk.web/content/_index.md

80 lines
1.8 KiB
Markdown
Raw Permalink Normal View History

2024-06-03 16:16:30 +02:00
+++
+++
## why?
lets take this script:
```bash
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
2024-06-04 02:00:50 +02:00
so generally, you would want to check if the dependencies of your bash script are installed, right?
2024-06-03 16:16:30 +02:00
you could write up something like this and put it in the start:
```sh
if ! command -v curl; then
echo no curl!
exit 1
fi
```
2024-06-04 02:00:50 +02:00
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.
2024-06-03 16:16:30 +02:00
you can use it like this:
```sh
#!/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`
2024-06-04 02:00:50 +02:00
it would output something like this:
```sh
#!/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."
2024-06-22 13:24:13 +02:00
exit 1
2024-06-04 02:00:50 +02:00
fi
unset non_ok
unset deps
# Dependencies are OK at this point
curl https://url.com
```
---
2024-06-22 13:24:13 +02:00
[website's source code available here](https://git.blek.codes/blek/bshchk.web)