multi-account-containers/bin/commons.sh
Aryeh Hillman 4a9bc37a46 Use /usr/bin/env to find bash
This enables `npm test` to successfully call scripts in `bin/` with
the `bash` binary specified by `PROFILE`.

---

Additional information:

In addition to potentially preferring an alternative version of bash
than the one in `/bin/bash`, some distributions forego storing bash in
`bin/` at all, such as `NixOS`:

```bash
$ cat /etc/os-release | rg '^NAME=' -r ''
NixOS
$ which bash
/run/current-system/sw/bin/bash
```
2023-11-17 15:55:12 -08:00

54 lines
1,009 B
Bash

#!/bin/env bash
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
printv() {
if [ -t 1 ]; then
NCOLORS=$(tput colors)
if test -n "$NCOLORS" && test "$NCOLORS" -ge 8; then
NORMAL="$(tput sgr0)"
RED="$(tput setaf 1)"
GREEN="$(tput setaf 2)"
YELLOW="$(tput setaf 3)"
fi
fi
if [[ $2 = 'G' ]]; then
# shellcheck disable=SC2086
echo $1 -e "${GREEN}$3${NORMAL}"
elif [[ $2 = 'Y' ]]; then
# shellcheck disable=SC2086
echo $1 -e "${YELLOW}$3${NORMAL}"
elif [[ $2 = 'N' ]]; then
# shellcheck disable=SC2086
echo $1 -e "$3"
else
# shellcheck disable=SC2086
echo $1 -e "${RED}$3${NORMAL}"
fi
}
print() {
printv '' "$1" "$2"
}
printn() {
printv "-n" "$1" "$2"
}
error() {
printv '' R "$1"
}
die() {
if [[ "$1" ]]; then
error "$1"
else
error Failed
fi
exit 1
}