It isn't even pure bash, if it uses external commands like `bc` and `sed`. It should probably limit itself to posix shell (or bash, if they need to) and coreutils.
They are only listing `bc` and `bash` as a prerequisite, but the example uses `sed`, so this is also not complete. So a full list of all required tools would be the first step.
But I am working on embedded systems where I write lots of POSIX shell scripts that run in tiny initramfs, etc. so I am very picky with my dependencies. If I had a better language to target busybox, that would be welcome as well.
I tested this a bit now, because I was curios, so it also requires `sudo` for the installation, installing it as `root` or as user with write permissions to `/opt` will not work and causes the misleading error:
Please make sure that your user can access /opt/amber directory.
Creating this directory, either manually or by the script before it fails to download amber, will cause the installer script think that amber is already installed. So not a very high quality shell installer script for a tool that generates shell scripts.
if age < 18 {
echo "I'm not an adult yet"
} else {
echo "I'm an adult"
}
compile to this:
__0_age=30;
if [ $(echo ${__0_age} '<' 18 | bc -l | sed '/\./ s/\. \{0,1\} 0\{1,\}$//') != 0 ]; then
echo "I'm not an adult yet"
else
echo "I'm an adult"
fi
instead of this:
__0_age=30;
if [[ ${__0_age} -lt 18 ]]; then
...
If you're going to compile to Bash, then use Bash-isms.
EDIT: `-gt` is POSIX, but tbf if there's no input sanitization, then bash (or sh) will choke on the float. In that case, as long as you aren't trying to round, you could use parameter substitution to truncate:
__0_age=17.6;
if [[ ${__0_age%%\.*} -lt 18 ]]; then
...
EDIT2: TIL that parameter substitution is POSIX [0] Section 2.6.2
There might be reasons to use `bc` like this if you don't know the type of `age` and it could be a non-numeric string. Buuuuut a programming language where a simple integer-comparison leads to two subprocesses is going to be slower than the slowest existing programming languages, by orders of magnitude.
// Define variables
let name = "John"
let age = 30
// Display a greeting
echo "Hello, my name is {name}"
// Perform conditional checks
if age < 18 {
echo "I'm not an adult yet"
} else {
echo "I'm an adult"
}
So there is no input, everything is known statically. No sanitation required. Any additional checks done by amber in the bash code is unnecessary.
So you want it to compile to POSIX sh but you're unhappy it doesn't use more POSIX-incompatible features? Maybe full POSIX sh compat is WIP, or in some simpler cases maybe it already is?
As I've just updated my comment to reflect, TIL that parameter substitution is supported (with some exceptions) by `sh`.
More broadly though, yes, there's a tendency for people to pop sub-shells without giving it a second thought. Those aren't free; use shell built-ins whenever possible!
The alternative stance is that if it's already bad, can't hurt to make it worse ;)
Something you could do to alleviate this is to interleave comments with the original Amber source, along with line numbers (which would balloon the script size, but probably not a huge deal).
Edit: you could also bundle a tool to recover the original Amber source from those comments, to allow for easy in-the-field script edits and/or audits.