-
Notifications
You must be signed in to change notification settings - Fork 46
bash boilerplate
This page list code comparisons between bash and Next Generation Shell, specifically ones that are longer in bash (or longer when implemented correctly as opposed to usually).
Note this is WIP.
In many bash scripts, we see something like this function:
exit_with_message () {
echo $1 >&2
exit 1
}
Specifically this snippet is from alexzautke/install-fhir-packages/install-fhir-packages.sh
In NGS, code that is very similar to exit_with_message
is already in the standard library. It means that code like exit_with_message
should never appear in your NGS script. In this particular comparison, it's not that NGS code is shorter; it doesn't exist.
Exit with the given message:
-
exit("your own message")
to exit with code 1 or -
exit("your own message", YOUR_EXIT_CODE)
to exit with the given code
Documentation: https://ngs-lang.org/doc/latest/generated/exit.html
Exit without message:
-
exit()
to exit with code 1 or -
exit(YOUR_EXIT_CODE)
to exit with the given code
There is also die()
in NGS.
Exit with message and backtrace:
-
die("your own message")
to exit with code 1 or -
die("your own message", YOUR_EXIT_CODE)
to exit with the given code
Documentation: https://ngs-lang.org/doc/latest/generated/die.html
Sample output
$ ngs -e 'die("This did not work out")' 2>&1 | sed 's/.*\] //'
+-----------------------+
| This did not work out |
+-----------------------+
Exception of type FatalError occurred
Info: message = (root) = String of length 21: This did not work out
Info: exit_code = (root) = <Int 1>
Frame 0: <builtin-stdlib>:9202:2 - 9202:35
Frame 1: <builtin-stdlib>:9176:21 - 9176:30 [in bootstrap_exception_catch_wrapper]
Frame 2: <builtin-stdlib>:9084:12 - 9084:24 [in bootstrap]
Frame 3: <command line -e switch>:2:1 - 2:4
Frame 4: <builtin-stdlib>:5605:31 - 5605:41 [in die]
+-----------------------+
| This did not work out |
+-----------------------+
In exit()
and die()
above, the given status is converted to exit code using ExitCode()
, which is defined for most built-in types. This allows the following examples to work.
instances = [{"status": "running"}, {"status": "wat?"}]
exit(instances.any({"status": "running"})) # exit code 0
instances = [{"status": "running"}, {"status": "wat?"}]
exit(instances.any({"status": "terminated"})) # exit code 1
instances = [{"status": "running"}, {"status": "wat?"}]
exit(instances.len() >= 1) # exit code 0
NGS official website is at https://ngs-lang.org/