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[0]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V...
> Maybe full POSIX sh compat is WIP
Still - I sure hope it's possible to do a comparison in sh without spawning two processes.
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!
Input sanitizing should be done before trying to hand inputs over to be summed.
// 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.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.