4 ms·
I believe Google just released https://github.com/google/zx https://github.com/google/zx. I am looking to refactor some "gnarly" and unreadable bash scripts in
by nodesocket 5y ago
I believe Google just released https://github.com/google/zx https://github.com/google/zx. I am looking to refactor some "gnarly" and unreadable bash scripts in JavaScript and wondering what everybody thinks is the best option?
Biggest pain points in my opinion in pure bash is flow control, string manipulation, arrays, and looping.
- sprobertson 5y agoWith the big caveat that I've tried neither, I'd probably go for zx if flow control is your main pain point. It seems Shelljs is more about adding shell to your node script, while zx is about adding node to your shell script.
- freedomben 5y agoIt's been my experience that for soe reason otherwise good developers who use functions liberally to modularize and DRY their code in <language X> seem to throw good software engineering priniples out the window when it comes to Shell. You'd be amazed at how readable and un-gnarly most shell script become if you break them into functions. Control flow and loops become quite easy. String manipulation on the other hand, if awk and jq can handle it reasonably well I'll use that, but I also forgive people for using a better scripting language. I prefer Ruby but I think using the language of the project is a better choice.
- krono 5y agoOh you hit the nail on the head! I have written some really basic shell scripts, and I try to use common shell modules/commands whenever it makes sense. It's just so confusing sometimes. The main challenges I faced in the beginning where caused by MacOS. MacOS comes with an older version of Bash, its default shell ZSH comes with its own slight variations on how it expects you to do things, and the utilities that come with MacOS aren't Gnu so the available options/arguments are not what you would find online. Then there is so much conflicting information online! A lot of it is also ancient, as in newsgroup archives from the 90s. String manipulation. When I look up how to do some specific replacement again, I come across all these massive inline awk scripts. But then I remember I could also just use sed and regex, or in some cases even just standard variable tricks, and achieve the desired result with 90% fewer code. The bigger scripts I've written are a mess.. I think. I still haven't been able to figure out how to error exit out of the entire script from a function that was called within it... :)
- lobstrosity420 5y agoI think zx is a neat project but it's important to clarify that it is not endorsed or attached to Google in any capacity. The Google account on GitHub is open for Google employees to post their pet open source projects, but they are otherwise entirely the responsibility of those employees.
- diveanon 5y agoI would take a look at pulumi if you are looking to infra as code in js / ts
- contriban 5y agozx is related, but has completely different use cases. ShellJS is meant to be used from CLI, as native shell commands; zx is meant to run shell commands from JavaScript. There might be some overlap in usage, but IMHO ShellJS is useless unless you use Windows, while zx is useless if you know how to write bash code.