6 ms·
In Unix shell makes it in 2, with the bang line: #!/bin/sh sort < $1 > $2 My preferred scripting language ;)
by reirob 13y ago
In Unix shell makes it in 2, with the bang line:
#!/bin/sh
sort < $1 > $2
My preferred scripting language ;)
- pestaa 13y agoAbsolutely yes, up to the point you're working with text. When objects (either implicit or explicit) start to interact with each other, shell scripting falls short quickly.
- kamaal 13y ago'Text' covers a very large domain. Once you get any data to a ordered text form, Unix command line text processing utilities have literally zero competition when it comes succinctness, power and how quickly you can create solutions. 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.
- aboodman 13y ago> 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!
- GRMrGecko 13y agoI like perl because of how it looks similar to php, and because it looks similar to php, the structure is somewhat like C. I am not sure how good Python is at text manipulation, but I'll bet it is similar to perl and can do, if not all the things perl can, most things perl can. 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.
- com2kid 13y ago> When objects (either implicit or explicit) start to interact with each other, shell scripting falls short quickly. 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!)
- rofrol 13y agoyou don't need hashbang if you run it the same like python: 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 scripts
- 6ren 13y agofaster too $ 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).
- tyrion 13y agoIt is faster mainly because of the startup time of the python interpreter. You can improve performances using the "-S" on the python interpreter. I get inconsistent results with time, I think maybe because of context switching as stated here [1]. See my results here: http://pastebin.com/HMUErzee http://pastebin.com/HMUErzee 1: http://stackoverflow.com/questions/9006596/is-the-unix-time-command-accurate-enough-for-benchmarks http://stackoverflow.com/questions/9006596/is-the-unix-time-...
- rofrol 13y agoMy results http://pastebin.com/C1crTA1S http://pastebin.com/C1crTA1S Code from https://github.com/rofrol/go-examples https://github.com/rofrol/go-examples
- tyrion 13y ago> Yep, bash is more readable for small scripts I 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))
- usea 13y agoRequisite C# one-liner: 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.
- frozenport 13y agoYou are cheating because of all the menus you had to jump through just to make the project :-)
- foobarred 13y agoUse LinqPad then it's not cheating. It becomes: File.WriteAllLines(args[1], File.ReadAllLines(args[0]).OrderBy(x => x));
- usea 13y agoI did actually use LINQPad. My comment was in jest :P 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.
- tyrion 13y agoI may be wrong but I think that this is actually spawning a subprocess and thus is not an exact translation of the python example. 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)
- greyfade 13y agoIt doesn't have to spawn a subprocess. Some shells (like those in Busybox and Toybox) reduce common POSIX userland tools to a function call.
- robotresearcher 13y agoRight. And even it was implemented as a subprocess, it's still idiomatic shell code, i.e. what you would Actually Do.
- reirob 13y agoSo what? The Unix Shell library consists of all the executables that are on the system - for me this is the beauty. There is not much of abstraction between the Unix Shell and the system under it. And shell is very tolerant to other programing languages as well - as long as they allow to represent data in text form and through streams or files ;)
- tyrion 13y agoYes, I know that this is the correct way of doing it in bash. I posted this because someone might test the speed of the two scripts and conclude "bash is faster" while they actually measured the speed of "sort" probably.
- judk 13y agoMicrobenchmarks are a distraction. Macrobenchmarks matter. 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.
- bcoates 13y agoIt'll work on files (much) larger than RAM, too.
- facorreia 13y agoDon't forget the quotes... sort < "$1" > "$2"