Skip to content

Instantly share code, notes, and snippets.

@amake
Last active July 1, 2023 01:31
Show Gist options
  • Save amake/e090d51fb894b401e2c140bed4a619b6 to your computer and use it in GitHub Desktop.
Save amake/e090d51fb894b401e2c140bed4a619b6 to your computer and use it in GitHub Desktop.
CLI tool-installing wrapper
#!/bin/sh
if [ $# -lt 1 ]; then
echo "Usage: $0 <tool> [args...]"
exit 1
fi
tool="$1"
shift
if ! command -v "$tool" >/dev/null; then
(apt update && apt install -y --no-install-recommends "$tool") <&-
fi
exec "$tool" "$@"
@amake
Copy link
Author

amake commented Jul 1, 2023

A wrapper for a tool that might not be installed, such as zstd. The tool will be installed if not present. Importantly, both args and stdin are properly passed to the wrapped tool even when installation happens. The magic is <&- which closes stdin for the installing subshell so that the original stdin is not consumed.

Mostly useful for e.g. CI where you want a particular tool installed on-demand but don't want to have to provide your own runner image.

Assumes the tool binary name and the APT package name are the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment