Live data from Hacker News

Bashing the Bash – Replacing Shell Scripts with Python (2017)

medium.com

81–90 of 138 posts

Re: Bashing the Bash – Replacing Shell Scripts with Python (2017)

#81
I didn't even read the article. The reason is that it depends what you need to do, from there you should decide what's the best tool to solve something. Recently I've started to automate all the package installations and configurations for some of the machines I have. I can't even imagine doing that in Python despite I've been using Python for a long time. I don't know that much about bash, but I got everything I needed and expected from bash scripts for the task I needed to do.

Yes, there are moments when you try to solve something in bash that you think is trivial in other languages but with bash is painful. Few days ago tried to test if a value is present in a bash array using a function. Couldn't get it working after trying few things. Then I realized I could perform a very similar (and good enough) check just seeing if a directory and a file do exist in my relative path to the script I was running: `if [-d DIR ] && [ -f FILE ]; then ...`.

So I needed to change my mindset a bit. Another example of where bash is really good is for tiny small tasks: I wrote a small script to increase/decrease volume that is coupled in i3 and calls the `mixer` command in FreeBSD. Works perfectly and never touched once it was working. Another one: I get an acoustic alert when my battery drops down below certain percentage. And a couple more: set a random background every time I log in to i3, download a page for offline reading.

Again, doing any of these things (or similar ones) with something different than bash (or maybe your favorite shell lang) seems like picking up the wrong tool for the job. There was a time when I cursed shell scripting a lot and that was because I couldn't see when to use shell scripting.

EDIT: Paragraph spacing.

Re: Bashing the Bash – Replacing Shell Scripts with Python (2017)

#82

I have some bad news for you, from a long, long, long time CS/EE/Sysadmin person: I can probably run my bash script on every linux I ever touched. I can probably have that python code break on half of the linux boxen I use. This one does not have module X installed. This one is too old, this one is too new, this python is not holding its mouth just at the right angle to work. Yeah, bash bash all you want. I have pull…

>I can probably run my bash script on every linux I ever touched. Exactly THIS !

yeah python (and before perl with cpan) had these issues that maintaining a consistent system is more effort than writing the damn thing in the first place. I loved Perl but tried to avoid modules like the plague. And python is almost worse because the same mentality (avoid modules) was abandoned by most of the community so now you get incompatibilities with what the OS provides and what gets installed via pip.

Perhaps it's unfair to pick at python for this when many of these issues are not the language but because people picking python when they should have just used shell script.

Re: Bashing the Bash – Replacing Shell Scripts with Python (2017)

#83
post #46

Earlier quoted context omitted.

But which one is more likely to also run on Windows? /s

... Hilariously, I think shell might actually win that; neither is installed by default, but you get some sort of bash/sh with any of interix, WSL1/2, git, cygwin, or mingw.

isn't WSL like a hypervisor that runs a guest? I'm clueless about Windows but when I start a WSL it is usually some form of Debian or Ubuntu that I can download from the Microsoft "store". Therefore isn't the shell you have whatever comes with that guest?

Re: Bashing the Bash – Replacing Shell Scripts with Python (2017)

#84
post #48

Earlier quoted context omitted.

> I was hoping someone finally had a library or something to make python good at the things shell does well (mostly, running stuff and piping output around). If only. Good news, they did! Even better news: it doesn't require python, it's cross platform, and supports an easy-to-read verbose style for scripts but a quick-to-write terse style for interactive usage. It's an absolute joy to use for nested data structures,…

What is Powershell

I just added powershell support to my Debian.

  $> ll /bin/powershell
  lrwxrwxrwx 1 root root 9 Mar 26 14:40 /bin/powershell -> /bin/dash

  $> cat ~/pwr_to_the_people.sh
  #!/bin/powershell

  echo "Hello Powershell"


  $> powershell ~/pwr_to_the_people.sh
  Hello Powershell
Works like a charm!

Re: Bashing the Bash – Replacing Shell Scripts with Python (2017)

#86

The one thing about Python that makes it so much more compelling over Bash is that thing you discover you need once your script starts getting long and repetitive: functions, with arguments, and return values. Python does this much better than Bash. It’s the one thing that makes me no longer use Bash. Oh and exceptions. Exceptions! The TWO things about Python that make it more compelling are functions and exceptions.…

> And speed. SPEED! Maybe I'm just a terrible python programmer and also all those around me suck at it but in 20+ years I have yet to see a well optimized python script faster than a well optimized bash/ash/csh/ksh/zsh script. Maybe your point is true if a script constantly does foo=`some thing` and constantly shells out other commands.

I was thinking of the time I calculated IPV6 EUI64 suffixes in bash for a kind of janky IPAM thing, and how even the barest minimum of arithmetic turned “instantaneous” into “takes seconds”.

The bash-centric solution was to write a little eui64.c and call that from the script. Bash isn’t for doing things, it’s for making other programs do things. That worked fine for a bit, but in the end it just became easier to factor and reason about the project when it wasn’t a weird mixture of clever things, and one Pythonic lump of boring and predictable things.

Re: Bashing the Bash – Replacing Shell Scripts with Python (2017)

#87
post #60

I have ksh scripts I wrote over 20 years ago on xenix, which still work today on bash or ksh. Meanwhile I have python apps that didn't make it a year. Any particular version of Python is a saner choice, if you could pick one and freeze it retroactively for the last 20 years and for at least the next 20. But that is not how Python works. Python breaks every 11 minutes. I would not write anything in Python that I cared…

Over the past decade I've been drifting towards doing more and more in bash that I'd've normally done one one language or another just because of this stability. Perl is another candidate if I need something more complicated, but I'm not as familiar with it so I don't really reach for it often. Are there any other languages with the same stability that would also fill this niche?

For shell-scripting sorts of tasks, there’s also awk which I find a nice sort of halfway between bash and Perl.

Also, I’ve been using various lisps (elisp in emacs org-mode and Common Lisp) and they both can be written in ways that will probably last a long time.

Re: Bashing the Bash – Replacing Shell Scripts with Python (2017)

#88

I have ksh scripts I wrote over 20 years ago on xenix, which still work today on bash or ksh. Meanwhile I have python apps that didn't make it a year. Any particular version of Python is a saner choice, if you could pick one and freeze it retroactively for the last 20 years and for at least the next 20. But that is not how Python works. Python breaks every 11 minutes. I would not write anything in Python that I cared…

That's what Python2.7 is and why every major proprietary product won't adopt anything else. It's also why it won't go away. All of my Python 2.6-2.7 scripts haven't been touched in 10 years in production and they aren't likely to ever be updated. I actually now refuse to write new Python that isn't compatible with both 2&3 for this reason. Python3 refuses to stabilize.

I’ve been slowly embracing the mantra “only use unmaintained software”: maintenance is great when it’s fixing bugs and such, but eventually people insist on breaking backwards compatibility.

Re: Bashing the Bash – Replacing Shell Scripts with Python (2017)

#89

I was hoping someone finally had a library or something to make python good at the things shell does well (mostly, running stuff and piping output around). If only. This really just reads like someone who doesn't know shell and does know python, but thinks the problem is the tool. They continually describe shell with phrases like "obscure and difficult-to-predict" while talking through things that are either obvious…

After going over it again, I think I can be more precise: What annoys me is that it's inconsistent to the point of being disingenuous. Like... You want to say error handling is hard to do right? That's fine, it is. You wanna write "Checking the status of programs using $? can be accidentally left off, leading to inconsistent behavior" and then in the same article write in explicit exception handlers , and act like so…

If you forget an error handler in python, execution terminates. So I don't see your point.

Re: Bashing the Bash – Replacing Shell Scripts with Python (2017)

#90
post #73

Earlier quoted context omitted.

> To say "you're holding it wrong" is just folly. Bash is worthy of the same treatment that "A Fractal of Bad Design" gave PHP, and it has no place in anything that wants to call itself engineering. is this a cloud AMD64 opinion that ignores the 2 trillion devices which Gartner predicts will never run python? I recently finished a ~600 lines ash script and the kids at work which just graduated complained that *"Y u n…

Judging by the reaction of the kids at work, what you did is already unmaintainable. Plus, if you are running shell out of necessity on a constrained system, that doesn't mean shell deserves to be a programming language where there's actual choice.

Anything is unmaintainable if the maintainers don't have the foundational skills.
Post reply on HN