6 ms·
I shamelessly reach for the backtick shell notation in any exploratory Ruby code that needs it. I try to replace it with something written in Ruby if the codeb
by dpritchett 13y ago
I shamelessly reach for the backtick shell notation in any exploratory Ruby code that needs it. I try to replace it with something written in Ruby if the codebase grows beyond my laptop, but backticks never get old.
=> 1.upto(100) { |n| puts `curl -s fizzy.heroku.com/#{n}` }
1
2
Fizz
4
Buzz
...
- manish_gill 13y agoHa! That backtick is the only reason most of my "scripts" are being written in Ruby instead of Python. :)
- sauravc 13y agoSeems pretty easy in python: from subprocess import call call(["ls", "-l"]) or import os os.system("ls -l")
- deleted 13y ago[deleted]
- tomsthumb 13y agoWhile there are several ways to skin that cat, the suggested way is to use subprocess.Popen . Also, when using Popen, calling .wait() can cause problems if you are expecting large amounts of info back from stdin or stderr. Using .communicate() is generally better.
- shawabawa3 13y agoThe difference between "pretty easy" and "easy" changes everything. Compare from subprocess import call call(["ls", "-l"]) to `ls -l`
- bashinator 13y agoI really like the python sh library for stuff like this. Would you rather do what you wrote, or this? for listing in ls('-l'): '''etc''' Here's the library in question - https://pypi.python.org/pypi/pbs https://pypi.python.org/pypi/pbs - it makes me super happy to do shell work in python.
- CatMtKing 13y agoWow, that's a neat library. You can just import any program and use it like a function (from sh import grep)! Thanks for the link. Also, sh documentation: http://amoffat.github.io/sh/ http://amoffat.github.io/sh/
- mkarttic 13y agoi use Sh to interface with my cmus player. from sh import cmus_remote! :)
- bashinator 13y agoYeah, it's replaced just a ton of boilerplate `def run(*args):` functions wrapping Popen. I couldn't be happier.