LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Can I take my bash scripts and make python versions of them? (https://www.linuxquestions.org/questions/linux-newbie-8/can-i-take-my-bash-scripts-and-make-python-versions-of-them-4175578250/)

CluelessJack 04-24-2016 08:20 PM

Can I take my bash scripts and make python versions of them?
 
I am new to python, actually I haven't started yet but have an interest in it. Can python execute linux commands and program like bash scripting does? Can python do what bash scripting does or better? Thx in advance

cnamejj 04-24-2016 08:56 PM

The short answer is yes, you can do anything a bash script does in Python. But IMO it's not worth it in much of the time.

Bash is just easier for some tasks. And if you learn "awk" and include that in your scripting, the cases where a shell script is a better option expands quite a bit as far as I'm concerned. Here's an example of why I think that's the case.

Capturing the output from a command in a Bash script might look like this:

Code:

output="$(..some command here..)"
rc=$?

whereas doing the same in Python without running into stupid problems might look like this:

Code:

try:
    cproc = Popen("..some command here...", stdout=PIPE, stderr=PIPE)

    out, err = cproc.communicate()
    rc = cproc.returncode
   
except OSError as __ex:
    err = __ex.strerror
    rc = __ex.errno

Both work but one it's a whole lot simpler to code. Of course you could make a general purpose function to run the commands, then import that module and not have to code it each time. I pulled that code from some code like that I wrote. But if you want to do something simple it's not always worth it to code things in Python IMO.

On the other hand, if the task at hand require doing something that would be tedious in Bash/Awk, like dealing with JSON data, then I would opt for Python every time.

For learning Python, I think picking a couple Bash scripts and coding equivalents sounds like a good idea. You'll probably learn a lot in the process.

CluelessJack 04-24-2016 10:53 PM

I think I will stick to bash scripting. I spent many years learning bash scripting and using sed, grep, awk in bash scripting. From your sample, it looks difficult to do an equivalent version of bash scripting with python. :)

grail 04-25-2016 12:12 AM

It also depends on exactly what type of things you are doing within your bash script. Whilst the example is correct, remember that python may be able to perform the same task itself just you
have to write code for it. As an example, if your bash script calls cat to display the output of a file, python can also perform this task and does not require cat at all.

It is also worth noting that some of the tasks re-written to be performed by python, or the like, may actually perform quicker than its bash counterpart.

cnamejj 04-25-2016 03:56 AM

I'd recommend giving Python a try. It's much easier to do some things Python, and some things are easier in Bash/Awk/etc... If you know both, you can pick the one that makes the most sense for the given task.

Plus while it's a little more complex, I find Python pretty intuitive once I got over the initial learning curve.

HMW 04-25-2016 06:23 AM

I agree with what has been said in this thread. Personally, if I find myself trying to "hack bash" to do something, that is the moment I switch to Python!

Give Python a try, it is my preferred language these days, except - as has been said above - when it's just easier and more efficient to write in bash.

Best regards,
HMW

sundialsvcs 04-25-2016 08:10 AM

I would politely disagree with the "stick to bash scripting" sentiment. I can count on one hand the number of bash scripts I have written in over thirty years.

Only one Unix shell actually had a built-in programming language that was meant to be one: the Korn shell (ksh). It was quickly realized that #!shebang was a much better concept. The built-in scripting capabilities of the ubiquitous bash (and sh) are quite minimal ... because they don't need to be sophisticated.

You can "shebang" any (other) language interpreter at the start of your program, and the shell will hand-over control to that (external) interpreter, and the user will neither know nor care. You therefore have at your disposal "an embarrassment of riches" of possible full-featured, made-for-the-purpose programming languages available with which to do anything you might need.

CluelessJack 04-25-2016 02:16 PM

I guess I will give python a try. There are many resources online to learn python. Thanks all


All times are GMT -5. The time now is 04:33 PM.