Page 1 of 4 123 ... LastLast
Results 1 to 10 of 36

Thread: Bash to Python

  1. #1
    Join Date
    Jul 2008
    Location
    $HOME
    Beans
    1,030
    Distro
    Ubuntu 9.10 Karmic Koala

    Question Bash to Python

    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!

  2. #2
    Join Date
    Jul 2008
    Beans
    1,706

    Re: Bash to Python

    Quote Originally Posted by Pro-reason View Post
    Does anyone have any tips for converting Bash scripts to Python?

    To my mind, it should even be theoretically possible to automate the conversion.
    anything is possible if you can think it...but anyway ya it probably can be done...if you know string parsing really well

  3. #3
    Join Date
    Jun 2008
    Location
    Sin City
    Beans
    588
    Distro
    Ubuntu UNR

    Re: Bash to Python

    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

  4. #4
    Join Date
    May 2008
    Beans
    Hidden!

    Re: Bash to Python

    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...

  5. #5
    Join Date
    Jul 2008
    Location
    $HOME
    Beans
    1,030
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Bash to Python

    Quote Originally Posted by Greyed View Post

    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.
    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!

  6. #6
    Join Date
    Jun 2008
    Location
    Sin City
    Beans
    588
    Distro
    Ubuntu UNR

    Re: Bash to Python

    Quote Originally Posted by Pro-reason View Post
    Well, duh. Of course the best thing to do is to port the result.
    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.

    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.
    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.

    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

  7. #7
    Join Date
    Apr 2007
    Beans
    14,781

    Re: Bash to Python

    Quote Originally Posted by Pro-reason View Post
    Does anyone have any tips for converting Bash scripts to Python?

    To my mind, it should even be theoretically possible to automate the conversion.
    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.

  8. #8
    Join Date
    Jul 2008
    Location
    $HOME
    Beans
    1,030
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Bash to Python

    Quote Originally Posted by LaRoza View Post
    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!

  9. #9
    Join Date
    Jul 2008
    Location
    $HOME
    Beans
    1,030
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Bash to Python

    Quote Originally Posted by Greyed View Post
    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.
    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!

  10. #10
    Join Date
    Aug 2006
    Location
    60°27'48"N 24°48'18"E
    Beans
    3,458

    Re: Bash to Python

    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.

    Quote Originally Posted by Pro-reason View Post
    I reckoned that a good way of learning the basics of Python would be to rewrite my shell 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.

    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.
    That is going to result in awful Python

    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.

    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.
    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)...


    Quote Originally Posted by Greyed View Post
    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.
    +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...

    Quote Originally Posted by Pro-reason View Post
    I'm anxious to have something that works straight away. I can optimise it later, as part of an on-going learning process.
    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

Page 1 of 4 123 ... LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •