1

Or it's meaningful only when you are in the command line and want something simple done right away.

Are these tools more efficient than a program library?

Quora Feans
  • 3,806
  • 7
  • 29
  • 46
  • hard to answer without an example. So my answer is: I have never seen a non-script program calling such a tool, they always used libraries. And there is no point why a command would be faster than a library. But there are good reasons to use libraries over external programs, e.g. dependencies. – Thorsten Staerk Jan 23 '14 at 09:59

1 Answers1

3

The programs often call the underlying program libraries you refer to. The commandline is there to chain these tools together which is much more efficient (in development time) then writing and compiling a program in C that calls the libraries. While only being marginally slower in execution time.

AFAIK this usage of small programs together was, and is, the Unix philosophy.

Timo
  • 6,202
  • 1
  • 26
  • 28
  • So, how would you use them from Python? g = os.popen("grep a *", "r") or through other means, like importing a lib? – Quora Feans Jan 23 '14 at 10:04
  • Specifically [The Art of UNIX Programming](http://www.catb.org/~esr/writings/taoup/html/) is probably the best on line resource for more info on [the UNIX philosophy](http://en.wikipedia.org/wiki/Unix_philosophy). – 41754 Jan 23 '14 at 10:22
  • 1
    (I assume) python already implements stuff corresponding to your examples internally -- these example (`tr`, `sort`, et. al.) are too simple to actually involve libraries -- but consider that `grep` links to `libpcre` (pcre = "perl compatible regular expressions). In fact neither perl nor python link to that, meaning they implement pcre's internally too, but they *could*, in which case the same library would be implementing pcre's for grep, perl, and python. You don't call these things directly, python does, and it provides you with an interface. – goldilocks Jan 23 '14 at 10:33
  • 1
    @QuoraFea You wouldn't. When you have a full-fledged scripting language you would use its native capabilities to replace external commands because this spares your script having to fork a shell and makes it more portable. In general, if you're writing Python, you only make a call to the OS when you absolutely have to. – Joseph R. Jan 23 '14 at 10:33
  • @QuoraFea I would use them from the commandline in daily use. I seldom write bashscripts, I use Python for that with the [plumbum](http://plumbum.readthedocs.org/en/latest/) library, much easier than os.popen() or subprocess.Popen(). Unless of course it can be done with the Python standard library or with PyPI installable one. – Timo Jan 23 '14 at 11:08