Comment on What are the recommended scripting languages for complex shell scripts beyond bash?
Knusper@feddit.de 1 year agoOh, I didn’t mean to say, you should throw out your shell scripts. For anything less than, say, 20 lines, they’re perfectly appropriate.
I’m saying, Rust starts to feel like a good choice from, say, 100 lines upwards, and I just don’t think, it’s worth bridging the gap between those two.
In particular, you can build a function that allows you to run commands without much boilerplate, e.g.: run(“echo hello | tee out.txt”);
(The implementation just appends that argument to Command::new(“sh”).arg(“-c”)
and runs it.)
That way, you can do the more complex things in Rust, whether that’s control flow or something like modifying a JSON file, without giving up the utility of all the CLI tools on your system…
Knusper@feddit.de 1 year ago
Somewhat of a weird addendum, but I actually only realized, you could port directly over like that, while writing the above comment.
Now I actually tried it on a 22 lines long shell script that I’ve been struggling with, and holy crap, I love it.
Like, I should say that I have always been (and likely will always be) shit at shell scripting. Any time I wanted to do a basic
if
, I had to look up how that works.As a result, even those 22 lines were ripe with code duplication and I always felt really unsure about what will actually happen during execution.
Well, rightfully so. While porting over, I realized I had a bug in there, which has been annoying me for a while, but I always thought, well, it is a shitty shell script. I still remember thinking, I should probably not implement it like that, but then leaving it anyways, because I felt it would become unreadable with the shell syntax.
Now it actually feels maintainable, like I can even easily expand on it.
And I have to say that
rust-script
is really smooth. I barely notice that it’s compiling during the first run after changing the script file, and it’s fully cached afterwards, so it executes instantly.I’ll still have to check for libraries that basically provide such a
run()
function/macro for me, but yeah, basically my threshold for not using shell scripts just dropped to any kind of control flow being involved.philm@programming.dev 1 year ago
Yeah the strict type-system of Rust is great at finding issues.
I think when understanding, that bash is basically only programs with parameters (
[
is a program that takes all kinds of parameters and as last parameter]
) then bash is quite ok for stuff that doesn’t need a lot of algorithms, i.e. passing the in and out from one program to another. But as soon as there’s basic logic, You’ll want to use a fully-fledged programming language.Also the maintainability aspect: You can just start using fancy stuff you never want to use in bash and it can slowly grow into a library or application or something like that.
Btw. I have started a syntax-sugar library/crate that creates typing information for all kinds of programs via the builder-type-state-pattern, so that you don’t always have to look up
man
etc. and that it should be more convenient to execute programs (not open sourced yet, and low priority for me as I’m working on various other exciting projects currently)