Live data from Hacker News

Shell Style Guide

google.github.io

221–230 of 336 posts

Re: Shell Style Guide

#221
post #82
post #75

Earlier quoted context omitted.

It's a 10-20 line Python script if you write it without any support code. If you're writing any amount of Python code that does shell things, you should either write or get an existing library. Also, it really isn't 10-20 lines to spit out the contents of some files in Python: >>> filelist = ["tmp.pl", "tmp.go", "Tmp.hs"] >>> for f in filelist: ... print(open(f).read()) It's trivial to code golf that down to 1 line,…

for i in ‘ls /tmp/foo’; do echo $i | grep bar | cut -d’-‘ -f3,4; done Is just so much easier to remember than Python’s OS library, right? I can intuitively string that together but can’t write python without looking up the docs and reading a paragraph about idiosyncrasies.

One nice thing about using a pipeline is that it will be multi-threaded by default. That is each process will have it's own thread(s) and distribute any work across multiple cores.

Also with something like Gnu Parallel you can easily scale across multiple machines. Maybe there are more efficient approaches but a pipeline and gnu parallel is pretty easy to scale up without thinking much about it.

Re: Shell Style Guide

#222
post #194

Earlier quoted context omitted.

You can do “/bin/bash -e” but not “/usr/bin/env bash -e”. Same for python etc. Good point about freebsd - I remember running into that exact problem. But I think I just symlinked it. https://unix.stackexchange.com/questions/29608/why-is-it-bet... has more info on the pitfalls. In my experience env causes problems, whereas explicit binary call always works (assuming the path exists). Not sure why I was downvoted.

Is this a problem with env? What does the "-e" flag do anyway? "man bash" proved unenlightening.

It's not because of env. It's just that shebangs are limited to passing one optional argument to the executable specified. The way they're parsed is the executable path, and if there's a space, then the rest of the line (including any additional spaces) is interpreted as 1 argument to the executable.

EDIT: -e causes bash to exit on the first command that returns with an error (returns with a non-zero return code). It's documented in `man bash` where it documents the `set` builtin command.

Re: Shell Style Guide

#223
post #81

Earlier quoted context omitted.

Exchanging Bash for Python is a folly. Python seems to handle complexity better, but only on the surface. (Basically Python has nice data structures, great string formatting, and .. that's it.) Setting up the Python environment requires something, because Python comes in many flavors (2 vs 3, system installed, user installed, /usr/local) and there are a bunch of factors (pip, pyenv, pipenv, PYTHONPATH, current direct…

I would be remiss if I didn't mention shellcheck, which will also warn you against doing stupid things. Very useful when debugging. https://github.com/koalaman/shellcheck

Also fantastic for asynchronous linting, if you're into that.

Re: Shell Style Guide

#224
post #103

Earlier quoted context omitted.

>Where is the reward when bash is present (almost) everywhere? It's really not. It's only present on some desktop Linux distributions. It's not present on almost any other OS or anywhere in the embedded space. POSIX shell, on the other hand, is everywhere.

Why wouldn't you install Bash then, if you want to run shell scripts on those platforms? I mean, what are the use cases? Where universality is important (so let's say when someone embeds scripts in Makefiles), I understand the need for POSIX, but other that I can't even come up with a good use case for having sh as default. (Maybe to save space, you ship the embedded device or Docker container with sh, sure, but then…

> Why wouldn't you install Bash then, if you want to run shell scripts on those platforms?

Busybox is a thing. Many embedded devices don't have the luxury of installing Bash–you're stuck with what you've got.

Re: Shell Style Guide

#225
post #127

> If performance matters, use something other than shell. I wrote a script that wrapped compiler calls to make dummy output files to debug a huge compilation. Since startup time was the most important metric, bash actually had by far the best performance.

People rag on bash a lot, but it now forms the core of a major deployment system I built at work. I'm not happy that it's in bash, but every other solution was worse, and it calls out to other tools (e.g. Ansible and Terraform) when they're appropriate. People have forgotten the subtle art of realizing what tool is best for the job. If all you're doing is stitching together other tools and doing some logging, bash is…

^ this!

Bash is the best stitching together language you'll get. Sure it sucks at scripting.. So stitch a script in there for God's sake!

Re: Shell Style Guide

#226
post #148

Earlier quoted context omitted.

> Not to put too fine a point on it, but that's nonsense. In PowerShell you pass structured data around instead of globs of text, which is much more maintainable. Using Bash on Windows is like going backwards, and it's sad that most developers don't even realize it. > Historically, PowerShell was supposed to be a replacement for cmd.exe and roughly equivalent to bash, yet suitable for the Windows environment. They tr…

Sad to see this downvoted. As a 20 year bash veteran: powershell is the first OS default shell that outputs structured data, and looking at objects and selecting the properties you want is a massive improvement than combining bash with sed/grep/awk to scrape text. Someone bizarrely responds that cmd still exists on Windows for compatibility purposes (though even Win+X starts powershell now) doesn't change this at all…

I'm always amused by these PowerShell threads on HN.

How is it that objects are an accepted thing for basically every programming environment in modern use, yet somehow controversial when it comes to the shell?

The prayer-based text parsing toolchain sucks. It has always sucked, regardless of platform. We put up with it because it was all that we had.

Jeffrey Snover came up with something better and thanks to PS Core we'll be able to use it everywhere!

Re: Shell Style Guide

#227
post #158
post #122

Earlier quoted context omitted.

I said Python wasn't going to be more concise. I said it has a variety of other useful properties. For instance, at least as I write this, you have "cut -d’-‘"; I assume you meant apostrophes (something getting too polite in a c&p, I assume) but the apostrophes are unnecessary, but you're so used to the compromises in shell I talked about you probably put those in there automatically. I'm not criticizing that; it's a…

It's hilarious in these discussions that every time someone comes to show how they can do the equivalent code in much less lines of bash there are always always always bugs of these sorts. And then the parent even specifically pointed out that files with spaces would be a minefield. Also complaining about looking up docs is just a matter of where you have your knowledge. I would have the opposite problem to understan…

You’re taking HN too seriously, it’s not a formal argument for nitpicking - it’s an opinion.

Re: Shell Style Guide

#228

I don’t understand all the hate for bash or python in this thread. These are programming languages. Like all programming languages one must understand how to deploy them and use them properly. Yes bash has faults absolutely. It can be very arcane and esoteric and I think this due to the fact that it’s creators are very much of the old 1970s and 1980s Unix mindset and largely bash tools still feel like they work the w…

It's because so many here on hn are the compsci theoritical programmer, sysadmins are dying devops is everything, kind of people who don't actually support production systems other than maybe their particular depts single page webapp, etc... Hn has a problem with people living in the SV filter bubble seeing everything through that lens... in the real world, bash is still a king. When your devs shit out some bad code…

Shell code can run in a whole bunch of environments that even getting python into can be tricky. initramfs before your drives are mounted, for example...

A lot of the discussion on here is mind-blowing, so many pushing to throw out perfectly good tech because it doesn't fit their (limited) worldview.

Re: Shell Style Guide

#229

Earlier quoted context omitted.

You seem to be imagining some sort of situation where bash is used to deal with input from customers (random users). You do not use bash for that. Filenames with spaces (lol) do not happen unless you do it yourself. Bash is useful for productivity when getting stuff done. For the work I do (where code never comes close to a customer and one-off tasks are common), if a candidate were to write a python script for the e…

> Filenames with spaces (lol) do not happen unless you do it yourself. This is a cop out. File names can totally contain spaces, and the difference between the Bash and Python solution is that one will continue to work when a file with spaces inevitably shows up and one will fail, possibly silently.

What do you do that causes filenames to appear with spaces? And why do you not fix the cause instead of forcing yourself to deal with the possibility of a filename with a space in every possible situation?

Re: Shell Style Guide

#230
post #105
post #69

I'm really surprised they went with: #!/bin/bash as opposed to: #!/usr/bin/env bash the latter feels more flexible and dependable for a script to be passed around.

I really don't like the `/usr/bin/env bash` approach (it came from the ruby world I think?). It's less portable than /bin/bash in my experience - I've been in environments, usually older ones, where it doesn't work at all. But /bin/bash works everywhere. You also can't pass command line arguments. And it's slightly more complex than just invoking /bin/bash.

/usr/bin/env anything is a pain to use with cronjobs
Post reply on HN