Reply to post: Re: Like grep? sed? awk?

Microsoft points PowerShell at Penguinistas


Re: Like grep? sed? awk?

Just to make something clear, I'm not here to bash (no pun intended) MS PowerShell. It has a narrower focus than unix-type shells (including bash) which are intended to be more general purpose than PowerShell. As long as a program reads from standard input and writes to standard output, it can be strung together in a unix shell whether it was intended to be used as such or not.

Someone has written a unix shell which uses JSON as the serialization format (as opposed to XML which PowerShell uses as its wire format), but it saw very little interest. It just didn't solve any problems which people felt needed solving.

I can understand why Microsoft went the way they did with PowerShell. They couldn't have ported a unix shell as is to Windows and expected it to work. While unix was designed right from the beginning to pipe text around and for everything to work together, Microsoft had to deal with the huge legacy issue of third party tools which didn't work in a consistent way. Their solution was to provide an "out of band" pipeline behind the scenes which third parties could hook into. They couldn't use the "in band" stdin and stdout (copied from unix) because too many third parties were spaffing random crap into those streams already. All in all, they took pretty much the only route open to them.

@h4rm0ny - "-xargs highlights like nothing else the inconsistency of the GNU/Linux versions. "

While you may find xargs to be difficult to understand, it's part of the standard toolkit which every unix/Linux/BSD/OS/X administrator is familiar with. What it let me do in this case is provide stat with multiple parameters, including some which act to modify the defaults.

@h4rm0ny - "-c switch for stat followed by "%U" to format the output as just usernames. Why does a utility for getting file parameters come with its own built in formatting tool?"

Yes stat has a lot of formatting options, but the alternative would be dozens of individual commands which I would have to remember which still wouldn't be able to as much. And let's be honest here, "Get-Acl" also has loads of parameters to choose from. It's inherent to the problem domain.

The advantage of using a formatting string is that I can do more than just things like "%U". I can combine the codes (more than 2 dozen just for files, plus more for devices) in multiple ways, and even include arbitrary text in the output. As an example "stat -c "%U some random stuff %a" *" will output the owner of the file, followed by the string "some random stuff", and then the access rights in octal format. I might be piping this output directly into a report which is to be read by someone rather than using it as input to more system administration scripts. I also don't want to invoke "stat" more than once per file when extracting multiple results, since that has a real performance implication which might affect other users. So, there are some very practical reasons why stat allows for complex behaviour.

@h4rm0ny - "my example is a bit clearer with regards to what is actually going on"

To be honest, I would have to dig through a lot of documentation and examples to even figure out how your bash example even works. Yes I can see that you are chopping up the output of "ls", but you are doing it in ways that I've never seen, let alone tried.

The problem with your example is that you are comparing apples to oranges. In your MS PowerShell example you are using the standard command for getting file information and processing the output of that, but in your bash example you used a command which was not intended for that instead of using the correct one, which is "stat".

Let's compare the two methods:

  • "find" is doing the same thing as "DIR"
  • xargs stat -c "%U" is donig the same thing as "Get-Acl | Select-Object Owner"
  • "sort | uniq" is doing the same thing as "Select -Unique"

By the way, I could have used "sort -u" instead of "sort | uniq", which would have made the two even more similar.

You could argue that 'xargs stat -c "%U"' is somewhat more complex than "Get-Acl | Select-Object Owner" in this particular example, but only to the extent that I had to use xargs, which is a command which unix admins are very familiar with. Yes I had to use -c "%U"', but Get-Acl had to use Select-Object and specify the parameter "Owner". It's very difficult to argue that stat is more complex because I used a parameter when you also had to specify a parameter for Select-Object. If you wanted to include arbitrary text in the MS PowerShell output, then I suspect the result would be even more complex again.

For either stat or Select-Object I would have to look up the documentation, since I seriously doubt I could remember all the available parameters for either.

So comparing like to like even with your own hand picked example I don't see any real advantage for Powershell in terms of ease of use.

POST COMMENT House rules

Not a member of The Register? Create a new account here.

  • Enter your comment

  • Add an icon

Anonymous cowards cannot choose their icon

Biting the hand that feeds IT © 1998–2019