#!/bin/sh
sort < $1 > $2
My preferred scripting language ;) #!/bin/sh
sort < $1 > $2
My preferred scripting language ;) Main(string[] args) { File.WriteAllLines(args[1], File.ReadAllLines(args[0]).OrderBy(x => x).ToArray()); }
Excludes using statements, .net library references. project file, solution file, framework config file...I have invented a language called Croml, in which all source code compiles to the same program. This program reads in a file, sorts the lines, and writes it back out to another file. An empty source file accomplishes this task.
File.WriteAllLines(args[1], File.ReadAllLines(args[0]).OrderBy(x => x));
However, there's no Main in LINQPad which takes string[] args. Also File.WriteAllLines takes an array as its second parameter, while OrderBy returns an IEnumerable.
sort < "$1" > "$2"Utility of shell goes down. Not because problems are represented as 'Text', but rather shell languages lack features like exception handling, proper error checking and many other things- Which make it difficult to write large programs in it.
As a next extension, you can learn Perl.
I assure you, after that you will not need anything ever.
I love it when people say things like this. As is their domain is the only domain in all programming.
For surely, you could write a competitive web browser in Perl. Or a first person shooter. Or a telecom system. Or a mars orbiter.
It makes you wonder why anyone ever invented anything else!
Languages I know are: C, Objective-C, Perl, PHP, Javascript, and bash. Languages I played in: Python, Ruby, C++, C#, lua, go.
I want to learn C++ as it has very good cross platform stuff. But don't know when to start. I want to learn python so I can help with mailpile, but don't know when to start. I want to learn go as I want to write my own chat protocol, but don't know when to start.
I am not biased with languages, I just don't know other languages and can't give a pros/cons comparing between them.
Not with PowerShell! PowerShell is amazing in how it takes the text pipe model of working and extends it to general purpose objects.
(Having access to all those .NET libraries is a nice bonus!)
sh script.sh
python script.py
So one line vs two lines import sys
open(sys.argv[1],'w').writelines(sorted(open(sys.argv[2]).readlines()))
Yep, bash is more readable for small scriptsI don't agree with that, in my opinion bash is more coincise while python is far more explicit and thus more understandable.
Python is not meant to be written in one line; the script could be rewritten in a readable way like this:
import sys
inp = open(sys.argv[1], 'r')
out = open(sys.argv[2], 'w')
out.writelines(sorted(inp))Including whitespace it's still half as long as the "11 line" core of the C++ script.
There's an unnecessary 'r'. Combined with the line rhythm established by opening the files in consecutive lines you ensure readers who would have been confused by the 'w' will quickly understand.
And the last line reads like programmer English; by which I mean English with a SVO word order. Not intuitive to the average person but quite parsable by anybody who's used a modern imperative language.
I can't even nitpick your choice of inp instead of input, the rhythm you setup and the contrast with 'out' means it's quite obvious what you mean.
Have an upvote.
import sys
reduce(lambda i, o: o.writelines(sorted(i)),
map(lambda args: open(*args),
zip(sys.argv[1:], ('r', 'w'))))
If Guido didn't despised FP so much maybe we could get a nicer lambda syntax... but here's anyway.I mean, this one line should be equally functional and .. it's shorter and even more understandable:
open(sys.argv[2], 'w').writelines(sorted(open(sys.argv[1]))
(Btw you're talking about a nicer lambda syntax but imho your example looks ugly because of all the unneeded stuff you've put into it) $ time sort < t > t-sh
real 0m0.016s
user 0m0.008s
sys 0m0.008s
$ time py -c "import sys; open(sys.argv[1],'w').writelines(sorted(open(sys.argv[2]).readlines()))" t-py t
real 0m0.088s
user 0m0.056s
sys 0m0.012s
BTW also a different result (I curled this page for the data).See my results here: http://pastebin.com/HMUErzee
1: http://stackoverflow.com/questions/9006596/is-the-unix-time-...
Code from https://github.com/rofrol/go-examples
This bash script rewritten in python should be something like this:
import subprocess, sys
inp = open(sys.argv[1], 'r')
out = open(sys.argv[2], 'w')
sys.exit(subprocess.call('sort', stdin=inp, stdout=out, stderr=sys.stderr))
(I don't know why but it seems that the performance of this version are worse than the original proposed example with writelines/readlines)The real problem with bash is the mess you get when you start needing whitespace or arrays or error handling or non tabular objects or a computation not already implemented as a system program.