[kwlug-disc] Sanity check of shell scripting approach
Doug Moen
doug at moens.org
Tue Apr 20 22:43:18 EDT 2021
I'd use jq for parsing JSON, so I'd probably write
$ jq .appDomain <manifest.json
instead.
Although that doesn't do a 'trim', which probably requires doing a regex substitution in jq (which I haven't learned how to do).
Because I don't know that, I'd probably resort to a shell trick instead for the trim, like this:
$ echo $(jq .appDomain <manifest.json)
Is that kind of shell trick insane? I dunno, whatever makes you productive and requires the least effort is good.
I recommend learning jq if you deal with JSON a lot, it seems easier than writing javascript code.
Doug.
On Tue, Apr 20, 2021, at 10:14 PM, Mikalai Birukou via kwlug-disc wrote:
> Sometimes, while in shell, I want to do manipulations that are simple in
> node, and I have no idea how to do in shell. I find myself with
> functions like:
>
> app_domain_from_manifest() {
> local manifest="$1"
> node -e "
> const m = fs.readFileSync('$manifest', { encoding: 'utf8' });
> const appDomain = JSON.parse(m).appDomain.trim();
> if (!appDomain) { throw new Error('appDomain field is not a
> string'); }
> console.log(appDomain);
> " || exit $?
> }
>
> Couple of lines passed to node -e.
>
> Is it insane?
>
> Do you do similar with python?
>
> What's your milage?
>
>
> _______________________________________________
> kwlug-disc mailing list
> kwlug-disc at kwlug.org
> https://kwlug.org/mailman/listinfo/kwlug-disc_kwlug.org
>
More information about the kwlug-disc
mailing list