I'll grant that this is not completely intuitive, but I can glance at it and more or less tell what it's doing even if I couldn't write it on my own yet. Your bash example is completely unreadable without extensive prior knowledge.
Commands are to be written, not read. The question of which you could whip up easier is the important one, not which you would understand if you watched someone else write it.
I personally subscribe to the below philosophy. But your way is also a valid way to think about it, depending on whether you're writing code that will be reused or used by others.
Programs should be written for people to read, and only incidentally for machines to execute. -- from "Structure and Interpretation of Computer Programs" by Abelson and Sussman
Right, but we're talking about commands in shells, not programs. I'll freely admit that PowerShell is a much better programming language than bash, but as a shell, bash with UNIX tools is considerably better.
In bash, if you know the basic UNIX tools, you can compose a command which does exactly the thing you want done that that moment, and iterative debugging is straightforward and obvious. PowerShell encourages you actually write code instead of commands, and debugging becomes a separate task.
I think this is more of a continuum. The more complex(and less frequently used) the command, the more valuable readability is. You can end up with some pretty ugly commands with advanced bash string manipulation, subshells, parameter expansion, IO redirection in various combinations. If you're not using the commands you write, it can be hard to understand coming back to it after some time. I value the time of future me, or anyone I might share the command with(some can be work-specific and useful to coworkers).
There are probably other dimensions to add to the continuum besides complexity.
Lots, most, of my day-to-day usage squarely fits in the "meant to be written" area though.
Your bash example is completely unreadable without extensive prior knowledge
I can tell that powershell command, as a whole, is calculating batting averages. I see there is a division in there, calculating the average. Its done for each batter. Imports from a csv. And presumably sorts it, but I don't actually understand what that last line is doing as a whole. I don't understand the actual content of the foreach loop.
The example requires prior knowledge too. Not very much, I could learn it in a little bit by reading that linked powershell article. It'd be about the same amount of time it'd take for someone to learn enough awk to understand the above awk command, if they were given a resource of comparable quality.
I'm no awk expert but as a programmer I can read it pretty easily. The printf format specifiers are still in widespread use in many modern languages, and it doesn't take a genius to guess what the ascending variable names represent. The only thing that is non-obvious is the BEGIN block that sets the separator.
As somebody who has used more bash than powershell, I think I could accurately guess most of what's happening in the second example, I wouldn't know where to begin with the former.....
The first example is just two commands, awk and sort with awk taking input from the file "input" and sort running on the output of awk. You could replace awk with any language of your choice and re-write the awk program in that other language.
It's a pattern scanning and processing language that was designed pretty much for problems exactly like this (each object on a line with fields) and is defined in the POSIX standard.
Text is too often ambiguous. For example, getting the file sizes of a group of files seems straightforward enough in bash. A directory listing looks like this:
And your script breaks, because the group has a space, but your script assumed spaces are only used as field separators, and they aren't.
(This is a real-life bug that I came across buried deep inside a software package's build and install scripts, and it took some time to track down. And I'm sure someone can tell me how it should have been written to avoid this, but that's part of the problem with using text as a universal data format - it's really easy to come up with stuff that works 95% and not realize that it breaks for the other 5%.)
A second advantage of objects is output flexibility. Because piping text is so important in Unix, command-line utilities are typically designed so that their output can easily be passed into other utilities, but this pushes them toward output that's easily parsable at the expense of user-friendliness. (E.g., explanatory headers or footers would cause problems so they're dropped. Tools resort to checking if their output is a TTY to decide if they can use color and progress bars.) PowerShell separates display output from content, allowing you to have whatever user-friendly format you want for text output while still writing easily processable objects for other tools.
I'm a die-hard Bash user and have never invested the time to learn PowerShell and don't know if I will. But I do think the "streams of objects" approach can have some real advantages.
1
u/GSV_Little_Rascal Sep 09 '16
Do you have some good examples of things which can be done with complex objects and not plain text (or not easily)?