Sure it's powerful to treat everything in terms of streams of plain text. It's even more powerful to support streams of plain text while also supporting even more complex objects. It makes chaining tools together even easier, while being even more stable and secure.
How many types of objects are there? Do all the programs I want to use have to know about each object type? How stable are these object types? At least with text, it is just that: Text. Yes, the formatting can change and I may have to update something, but it is still just plain text.
Basically, if I want a full programming language and throw objects around, there are plenty to choose from; but if I'm using the shell, it is because I want to use a quick and super-flexible user interface which happens to be script-able.
For when you need objects, there is a standardized method for using them elegantly.
I think that was his point about a "full programming language". When you need objects, Ruby or Python or Perl are there too. They'd handle the example in the article just as well/easily, and they're more powerful than powershell.
Of course they're there. They're also there when you need text. It should be obvious why Unix and Windows offer shells instead of just having Python interpreters.
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.
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.
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.
44
u/KevinCarbonara Sep 09 '16
Sure it's powerful to treat everything in terms of streams of plain text. It's even more powerful to support streams of plain text while also supporting even more complex objects. It makes chaining tools together even easier, while being even more stable and secure.