Hacker News new | past | comments | ask | show | jobs | submit login

Thirty lines? Unholy one liners are not limited to shell scripting:

  from subprocess import check_output
  sh = lambda script: check_output(script, shell=True, text=True)
  ips = set(line.split()[-1]
        for line in sh('last -a').splitlines()
        if line and 'tmux' not in line)
The first two lines are pure overhead.

The last line is the equivalent of a shell script one liner but now has all the advantages of a language that supports “\N{clown face}”.




> The first two lines are pure overhead.

In 200 bytes half of which is overhead. That's not a great start.

> The last line is the equivalent of a shell script one liner

It's slower and requires much much more typing, and anything I want to add isn't going to go in the right place. It has a gross hack to support "tmux", and produces other spurious output (bugs). I don't want my X sessions or other ptys; I have reverse DNS disabled, and I really just want IP addresses.

This is what I would write in shell:

    last -a|sed -e 's/.* \([0-9a-f]*[:.][0-9a-f.:]*\)$/\1/;t;d'|sort -u
> now has all the advantages of a language that supports “\N{clown face}”.

This is a joke right? I have twenty cores, this is going to use three. Python is going to use one. 30% less typing, less bugs, faster. Those things are important. A fucking emoji is not.


for those of us with not as much grey in our beards (+1 this response if you get the joke) the python example is a ton more readable to me. Now that probably doesn't matter if the utility is only for you. I've a number of helper programs/scripts/etc that I use that I wrote and are only for my consumption.

Re the paralell stuff and python it's easy to import multiprocessing and take advantage of all cores. I think where python wins is how easy it is to handle errors and organize things as it's a full fat programming language vs a shell scripting language like Bash + the gnu userland.


> the python example is a ton more readable to me.

The python example is also wrong, as in it produces the wrong output: Maybe if you are only worried about your own consumption you can ignore all those poor souls running literally any other application besides ssh and tmux, but at some point you're going to have to stop admiring how "readable" it is and fix it, and then what?

I don't buy that conservative coder mentality of keeping the code clean and readable, because one drop of shit is going to be impossible to remove later. In fact, I think that's bananas. Get correct first, then improve.

> Re the paralell stuff and python it's easy to import multiprocessing and take advantage of all cores

You jest.

> I think where python wins is how easy it is to handle errors

Pray tell what errors do you think we need to handle?


Your shell thing is honestly fine. The emoji thing was a joke, sorry that wasn’t clear.

The thing is with 1 IP lookup in N addresses a grep is also fine, but with M IP lookups the set is O(1) instead of O(N) and that makes a difference.

It’s not just theoretical. Bash slowness adds up fast and you will need to upgrade to a real language anyway.

Example: IPv6 addresses need to be truncated to /64 but last prints them with 0 truncation. Good luck doing that in bash. You’ll need a fully fledged IPv6 address parser, if it’s code that has to survive.

You make a fine point, honestly, about what you can get away with in shell scripting. The bigger picture is that literally nothing ever survives the sticky fingers if many engineers over time if it is a shell script. It gets worse and worse until it is ported, at which point it begins a new life as maintainable code.

Bash is a shell. Not a programming language.


> Unholy one liners are not limited to shell scripting

You’re mostly proving the parent’s point here. Yours isn’t a one liner, and even in Bash people don’t write multi-line for loops in one line. I don’t know what you mean about the first two lines being overhead; this doesn’t work without them, and you have magical options “shell” and “text” in there that matter. Your script doesn’t pass the result to another process, so you need another line. This also splits output on spaces, it should be more generic. Doing that involves another module (regex), another function call (or more likely several), and several more lines.

Compared to “last -a | grep tmux | process”, your script is much closer on a log scale to 30 lines than 1, even if the parent’s 30 is a little exaggerated, and even if we use the 5 lines you showed and not the ~10-12 lines it really would be.


I don't have exact rules, but if it's (1) quick, (2) simple to read/write in shell, (3) separate from my app's business logic, then I'll use a shell script.

For example, to take a line off of the end of a file, sed is pretty easy:

    sed -i '' -e '$ d' file.txt
After a few pipes, I will reach for Python. Regarding line count, after a `if __name__ == '__main__':` block and some helpful docstrings, it ends up being about thirty lines or so. "\N{man shrugging}"


https://xon.sh/ would make this so much better




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: