Does anyone have any tips for converting Bash scripts to Python?
To my mind, it should even be theoretically possible to automate the conversion.
Does anyone have any tips for converting Bash scripts to Python?
To my mind, it should even be theoretically possible to automate the conversion.
Last edited by Pro-reason; November 11th, 2008 at 11:37 PM.
If people were nicer, I'd answer more queries here!
In theory it is possible to convert any Turing complete language into any other Turing complete language.
It really isn't possible nor is it desirable. The methods that bash uses to do things don't map well to Python. However doing so would not make it a Python script as it would not use things in a Pythonic manner.
As for manual conversion that's simple. Take what the Bash script is doing and do that in Python. Don't port the methods of achieving the results, port the results.
A good example would be complex logic in bash that uses recursion to walk a directory tree. Bash has nothing similar to os.walk() (or the older, os.path.walk()) in Python. So instead of reimplementing the logic of the directory walk you'd reimplement the desired results using os.walk() as the mechanism.
Last edited by Greyed; November 11th, 2008 at 04:23 AM. Reason: Addition to answer the original question.
Warning: Any code examples I write are probably untested and contain bugs. Do not execute directly. Look for intent, not accuracy, please!
L.A.G. - Jobs Dissembles - 2010/4/29
I think you could.
Well, since Bash scripts are mostly just sequences of commands, I imagine most of them would be replaced with calls to a subprocess module. Builtins would have to be replaced with Python equivalents.
As to actually doing it, that would be tricky. In Bash, there is no difference between normal variables and environment variables. Python code would have to figure out which is which, and the answer may not always be obvious.
In addition, shell syntax is not the most easily parsed thing... (To be pronounced "par-sed". )
Optimizing it to replace things done in Bash that can be done more easily in Python would be quite difficult, involving all kinds of things way past me...
Well, duh. Of course the best thing to do is to port the result. The point is that I don't know how. I am well-versed in bash scripting, but I want to get a programming job. So, I plan to teach myself Python, and then stuff like Java and C. I reckoned that a good way of learning the basics of Python would be to rewrite my shell scripts.
So, I've changed “function” to “def”, “echo” to “print”, and suchlike. I'm looking for some sort of beginners' guide to Python for people who know bash but not other programming languages.
I also mentioned offhand that it would be cool if the conversion could be done automatically. Then I could examine the resultant code, and immediately understand it because it would still be my program. That's just a dream, though.
If people were nicer, I'd answer more queries here!
I didn't mean it to be snarky. I was being quite serious. A common mistake people make after learning one language but before learning another (and this goes for natural languages as well as programming languages) is to think in their first language and try to translate that verbatim into the second language. The problem is that not everything is a 1:1 translation which is why one should focus on the results, the intent, and not the steps to which that result was achieved. Believe you me, if I had someone tell me that when I was still on my first or second language I would have done a whole lot better.
Can't help you there. My extend of bash is to know enough to get the general idea of what it does and rewrite that in Perl or Python. I'd say focus less on trying to come at it from a bash perspective and just learn the language. To be honest you've already grasped the hardest part for lots of people; the shebang.So, I've changed “function” to “def”, “echo” to “print”, and suchlike. I'm looking for some sort of beginners' guide to Python for people who know bash but not other programming languages.
Go through the Python tutorial in the official docs. Also just leave an interpreter open in one window. Use it to solidify the basic concepts in your head. In that manner learning bash and Python are not much different since you can play interactively with both of them.
Warning: Any code examples I write are probably untested and contain bugs. Do not execute directly. Look for intent, not accuracy, please!
L.A.G. - Jobs Dissembles - 2010/4/29
Yes, rewrite the logic in Python. Bash scripts are usually short, and depending on the task, it may string together several programs with pipes. This leads to very short bash programs, which take more lines in Python.
Also, the simplest way, would be to use just put the bash script in Python using os.system(), but that is really silly.
Perhaps I could embed each Bash function in Python like that, and change each one to real Python as I learn the language. I'm anxious to have something that works straight away. I can optimise it later, as part of an on-going learning process.
One thing I currently do is run a command (e.g. gzip) and then have an if ... do ... fi which does one thing or another depending on the exit status of the command. How would I do that? (I'm not interested, at this stage, in whether this is the approved way of doing things.)
If people were nicer, I'd answer more queries here!
I'm a professional translator and language tutor, so you don't need to lecture me about that sort of thing.
I know from long experience how my own brain works when learning, and I know that a crude, 1-1 translation of my script into Python would aid me in comprehending how to do equivalent things in the new language. Later on, I can develop more elegant expressions of the possibilities of the language.
If people were nicer, I'd answer more queries here!
Sorry, but really, "ure doin it rong"...
What you are planning will just waste a humongous amount of time trying to hack Python into doing something that it is not suited for, that is, verbatim copies of bash scripts.
The only way to really properly replace bash as a system scripting language is to write Python, not bash in Python. I can't even begin to think how nasty it will be to run shell tools as subprocesses and manage the pipes under Python and do the parsing and sticking Python data structures somehow there in the middle... you are so much better off having your data as genuine Python data structures all along and calling into appropriate modules instead of shell tools.
Python isn't that hard and is actually a very discoverable language once you get the basics, and your plan actually expects you to be a very skilled Python hacker to begin with so that you can pull of this kind of a duct tape hackery approach.
That is going to result in awful PythonSo, I've changed “function” to “def”, “echo” to “print”, and suchlike. I'm looking for some sort of beginners' guide to Python for people who know bash but not other programming languages.
For people who know bash... well... basic ideas of programming have already been learned, so learning those other languages from scratch is the fastest way to get to grips with them.
Even if there was such a conversion, the converter would not understand your meaning, and the result would be horrible. I bet you couldn't understand it simply by virtue of it being your program, and it would help you in grokking Python all the less (which you by definition wouldn't understand)...I also mentioned offhand that it would be cool if the conversion could be done automatically. Then I could examine the resultant code, and immediately understand it because it would still be my program.
+1, and this ties in well with the general arguments we have about differences about languages... the claim that it's just skin deep syntax is just simply plain false regardless of the theoretical basis for having a translator between languages...
So if you want something that actually works straight away, write Python. This "optimization" could not be done piecemeal, it would have to mean a complete rewrite to have your data not in pipes between processes but actually in Python.
LambdaGrok. | #ubuntu-programming on FreeNode
Bookmarks