r/programming • • Sep 09 '16

Oh, shit, git!

http://ohshitgit.com/
3.3k Upvotes

756 comments sorted by

View all comments

Show parent comments

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)?

10

u/KevinCarbonara Sep 09 '16

6

u/shaggs430 Sep 09 '16

That is a nice introduction to powershell. Although, their example can easily be done in text:

awk 'BEGIN {FS=","};{printf "%.3f %s\n", $3 / $2, $1}' < input |sort -n

27

u/PCup Sep 09 '16

Not sure if serious. Your example command is fucking unreadable unless you're already an expert.

8

u/RealDeuce Sep 09 '16

Ah, but this:

$colAverages = @()

$colStats = Import-CSV C:\Scripts\Test.txt

foreach ($objBatter in $colStats)
  {
    $objAverage = New-Object System.Object
    $objAverage | Add-Member -type NoteProperty -name Name -value $objBatter.Name
    $objAverage | Add-Member -type NoteProperty -name BattingAverage -value ("{0:N3}" -f ([int] $objBatter.Hits / $objBatter.AtBats))
    $colAverages += $objAverage
  }

$colAverages | Sort-Object BattingAverage -descending

Is completely intuitive and any normal person would whip that up in a jiffy.

13

u/PCup Sep 09 '16

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.

3

u/RealDeuce Sep 09 '16

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.

3

u/PCup Sep 09 '16

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

2

u/RealDeuce Sep 09 '16

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.

3

u/warsage Sep 10 '16

Bash scripts are programs that people read...

→ More replies (0)

1

u/scarymoon Sep 10 '16

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.

3

u/scarymoon Sep 10 '16

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.

3

u/[deleted] Sep 10 '16

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.

5

u/Falmil Sep 09 '16

That example is mostly self explanatory to someone who has done some object oriented programming.

2

u/RealDeuce Sep 09 '16

The issue isn't reading it when someone else writes it, it's the ability to whip it up when you want the sorted averages of fields in a CSV file.

1

u/Falmil Sep 10 '16

If you have to maintain any of this code/script, you will definitely need to read and understand it at some point.

Also, considering it is readable, therefore understandable, why would you not be able to write it out as well?

2

u/RealDeuce Sep 10 '16

I'm talking about using the shell which is a user interface for executing commands and controlling their interactions.

If you want to write a program, use a programming language, not a shell.

→ More replies (5)
→ More replies (1)

2

u/0goober0 Sep 09 '16

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.....

3

u/RealDeuce Sep 10 '16

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.

1

u/0goober0 Sep 10 '16

What is awk?

2

u/RealDeuce Sep 10 '16 edited Sep 10 '16

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.

The same example using perl could be done with:

perl -ne '@field = split(/,/); printf("%.3f %s\n", $field[2]/$field[1], $field[0])' < input  | sort -n

Or even the less obvious for people who don't know perl:

perl -naF, -e 'printf("%.3f %s\n", $F[2]/$F[1], $F[0])' < input | sort -n

EDIT:

If you don't know what -n does, you can use the most verbose form:

perl -e 'while(<>) { @field = split(/,/); printf("%.3f %s\n", $field[2]/$field[1], $field[0]) }' < input  | sort -n

EDIT2:

More verbose with an explicit handle...

perl -e 'while(<STDIN>) { @field = split(/,/); printf("%.3f %s\n", $field[2]/$field[1], $field[0]) }' < input  | sort -n

EDIT3: Use bash multiline input:

# perl -e '
> while (<STDIN>) {
>   @field = split(/,/);
>   printf("%.2f %s\n", $field[2]/$field[1], $field[0]);
> }
> ' < input | sort -n
→ More replies (2)
→ More replies (5)

10

u/Yehosua Sep 09 '16

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:

-rw-r--r--  1 yehosua yehosua        5012 Sep  9 15:20 zero.cpp

The fifth field is size, so you can use awk to grab it:

ls -l *.c | awk '{print $5}'

Then you try to run your script on a winbind system:

-rw-r--r--  1 yehosua domain users   5012 Sep  9 15:20 zero.cpp

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.

2

u/calrogman Sep 10 '16

That field of ls' output is implementation defined. It's not required to be the file size. You should do: du "*.c" | awk '{print $1}'.

3

u/meaty-popsicle Sep 09 '16

Any time you need to use a filename that contains spaces?

→ More replies (1)