[HN Gopher] Easily handle CLI operation via Python instead of re... ___________________________________________________________________ Easily handle CLI operation via Python instead of regular Bash programs Author : EntICOnc Score : 112 points Date : 2022-02-11 16:01 UTC (1 days ago) (HTM) web link (github.com) (TXT) w3m dump (github.com) | VectorLock wrote: | I've been using a kind of hacky analog of this for JSON for | years, rather than messing around with jq, JMESpath, etc. | https://gist.github.com/craigcalef/89799df3f2fc7957d84a1b06a... | Naac wrote: | This looks like an unmaintained version of (the much more | complete) pyp: | | https://github.com/hauntsaninja/pyp | daenz wrote: | I love the motivation for this tool. This looks very useful for | when you just want to make something easier in an existing shell | script, without fully migrating to Python. If you _do_ want to | migrate fully to Python, I encourage people to use sh[0] | (disclaimer: author). | | One concern that I have is that it could be so useful that now | your script is filled with `| pz`, and is launching Python | processes at every line . Now you have a tangled hybrid of Bash | and Python. | | 0. https://github.com/amoffat/sh | LeoPanthera wrote: | That is a really confusing name, since /bin/sh is the default | shell in almost every unix. | dahart wrote: | This sh is a python lib, not a unix shell, so can't be easily | confused. | yunruse wrote: | Dealing with a lot of `| pz` statements could be pretty quickly | solved with eg a `pz` interpreter that can be explicitly given | multiple sequences of inputs. | | That said, it would be amazing if `pz` could take in some input | arguments and output the Unixy equivalent if it can find one. | Klasiaster wrote: | Or pz could be a lightweight wrapper that speaks to a python | server or starts a new one if there isn't yet. The server | could end after X seconds of not getting new requests. | brtknr wrote: | Probably easier to stick to bash rather than learn a new set of | commands from scratch. Also bash commands are universally | available whereas this needs to be installed first which is not | always that straightforward. Also python is slower at some of the | examples than compiled executables. | fivea wrote: | > Probably easier to stick to bash rather than the overhead of | learning new set of commands that I need to learn from scratch. | | That's all fine and dandy until your shell script grows beyond | a dozen lines of code or so. The moment that happens, shell | scripts become barely readable and terribly hard to maintain, | unlike alternatives such as python. | otikik wrote: | "Universal" is too much of a stretch. You still get tripped by | things like "that flag doesn't exist in OSX sed" or "that | option in git was only available in git > 2.8.14" | | And that's how you end up with bash scripts wrapped inside | Docker images, that I have been seeing for the last couple of | years. | joseph8th wrote: | And then there's `xonsh`, "The unholy union of bash and | Python."[0] | | Which I've been using as my daily driver for a few years, and | absolutely swear by it. | | https://xon.sh | seanw444 wrote: | What in the world... that's certainly _interesting_. | pedrovhb wrote: | I've been daily driving xonsh for a couple of years too. It | _sounds_ like a terrible idea for reasons you can't quite put | your finger on, but other than a minor annoyance every now and | then, it's been great. | | I especially like iterating over files as Path objects with the | simple syntax for f in p`.\*\.jpg`: | if f.isdir(): foo = $(echo @(f.absolute())) | suifbwish wrote: | What sorcery is this?! Object oriented bash? Incredible. | abnry wrote: | PowerShell has entered the chat. | kseistrup wrote: | Looks great. | | It could be even more useful with an `f` variable that holds the | 'words' obtained from splitting `s` at whitespace. | hauntsaninja wrote: | I wrote a similar tool a while back that lets you create your | own "magic" variables. I use `f` all the time! | https://github.com/hauntsaninja/pyp#pyp-lets-you-configure-y... | kseistrup wrote: | There it is! I knew I had seen something akin to pz. Thanks | for the link! | fmakunbound wrote: | I will try this, but I still have ptsd from the last time I | installed something python based. | uniformlyrandom wrote: | I don't know if I like this approach. Common "regular" Bash | programs have a huge advantage - they are common. When I share my | 'may accidentally conjure up a monster' one-liners with my team, | I am sure they can read it, understand it, and run it. I lose | that advantage with the use of a very nice, but obscure tool. | tyingq wrote: | Most of the examples would be relatively simple Perl or AWK | one-liners. The appeal of this tool seems mostly to be that you | can use Python, which isn't one-line friendly out of the box. | ogogmad wrote: | Bash involves tons and tons of arcania. It also doesn't support | features that PL editors have like Intellisense. I sometimes | feel like the only people who don't see a problem with the Unix | experience are people who've used it day-in day-out for a | decade, and don't need to grep man pages more than they'd like | to. But by then, it's Stockholm Syndrome? I think this Github | project might begin to pull out the chord. I know my Python | better than I know my awk/sed/bash/xargs/find/make/bc... | suifbwish wrote: | Bash has one major advantage though: it's core libraries and | and commands almost never change which mean your scripts are | most likely going to work forever. Python core library | development is filled with asshats who like to deprecate | naming conventions in later versions rather than just | improving the existing libraries and keeping the method names | the same. Python is great for a lot of reasons but if you | know bash well, it is far more reliable in the long term. | jamespwilliams wrote: | This seems very useful! | | The behaviour of auto-imports https://github.com/CZ-NIC/pz#auto- | import seems bit scary: | | > Caveat: When accessed first time, the auto-import makes the row | reprocessed. It may influence your global variables. Use verbose | output to see if something has been auto-imported. | | It sounds like this could cause some very subtle bugs. Maybe a | strong and clear warning when a line is being reprocessed would | help. | hauntsaninja wrote: | I wrote a similar tool a while back. My solution for this was | to statically analyse the input to discover unused names. The | cool part of doing transformations statically is that you're | able to see the exact Python code you'd run without running it, | e.g. `pyp --explain x[:5]`. https://github.com/hauntsaninja/pyp | gpvos wrote: | That's just perl -pe or -ne. | nomorecommas wrote: | Very cool, though I probably won't use it. | aasasd wrote: | Ah, yes, the Perlton. | freedomben wrote: | For people that know Ruby, if you haven't explored Ruby's CLI | abilities, you definitely, definitely should[1][2]. When I was | building my (free and open source) awk course[3] I fell in love | with awk. When I later found out that Ruby has some of the same | features, it changed my life: | | [1]: https://robm.me.uk/2013/11/ruby-enp/ | | [2]: https://benoithamelin.tumblr.com/ruby1line/ | | [3]: https://github.com/FreedomBen/awk-hack-the-planet | js2 wrote: | Perl too. The -p, -n, -e switches work identically. | | -n causes Perl to assume the following loop around your | program, which makes it iterate over filename arguments | somewhat like sed -n or awk: LINE: | while (<>) { ... # your program goes | here } | | -p causes Perl to assume the following loop around your | program, which makes it iterate over filename arguments | somewhat like sed: LINE: while (<>) | { ... # your program goes here | } continue { print or die "-p destination: $!\n"; | } | | https://linux.die.net/man/1/perlrun | p_l wrote: | Little underappreciated part of Ruby these days is that part | of it grew out of Perl, and specifically supports similar | kind of scripting for daily tasks. Pretty sure both thsoe | specific switches and some of the sigils used in syntax came | from this Perl legacy. | freedomben wrote: | Yes thanks, I should have added that perl took it (and | improved it) from awk, and ruby took it from perl. Most ruby | people don't like to remember that ruby has a lot of perl- | isms in it, but I think perl was a great language (and | probably is, though I liked ruby so much better I never | looked back). | nomorecommas wrote: | Yep, ruby is a natural, pipeline friendly, command line | companion out of the box. | | https://github.com/learnbyexample/Command-line-text-processi... | | https://learnbyexample.github.io/learn_ruby_oneliners/ | rasengan wrote: | https://github.com/realrasengan/jpipe | | A super simple version for JS | visarga wrote: | Nice, I made a similar tool for Perl. I chain them up to three | rows in terminal, if they get longer I switch to a script. ___________________________________________________________________ (page generated 2022-02-12 23:00 UTC)