LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-24-2016, 08:20 PM   #1
CluelessJack
LQ Newbie
 
Registered: Apr 2016
Distribution: Mint
Posts: 3

Rep: Reputation: Disabled
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

Last edited by CluelessJack; 04-24-2016 at 08:30 PM.
 
Old 04-24-2016, 08:56 PM   #2
cnamejj
Member
 
Registered: Mar 2015
Distribution: Ubuntu
Posts: 37

Rep: Reputation: Disabled
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.
 
Old 04-24-2016, 10:53 PM   #3
CluelessJack
LQ Newbie
 
Registered: Apr 2016
Distribution: Mint
Posts: 3

Original Poster
Rep: Reputation: Disabled
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.
 
Old 04-25-2016, 12:12 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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.
 
Old 04-25-2016, 03:56 AM   #5
cnamejj
Member
 
Registered: Mar 2015
Distribution: Ubuntu
Posts: 37

Rep: Reputation: Disabled
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.
 
Old 04-25-2016, 06:23 AM   #6
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
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
 
Old 04-25-2016, 08:10 AM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,673
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
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.
 
Old 04-25-2016, 02:16 PM   #8
CluelessJack
LQ Newbie
 
Registered: Apr 2016
Distribution: Mint
Posts: 3

Original Poster
Rep: Reputation: Disabled
I guess I will give python a try. There are many resources online to learn python. Thanks all
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
List of Bash/Vim/Python/C/Linux Tutorials/Guides and Scripts Leo-G Programming 5 01-19-2016 06:46 PM
[SOLVED] Why are you supposed to make bash scripts executable? Nbiser Linux - General 9 04-22-2013 02:20 AM
LXer: Python Scripts as a Replacement for Bash Utility Scripts LXer Syndicated Linux News 1 01-17-2013 08:08 AM
[SOLVED] Need help to make script work, combined bash & python Mr. Alex Programming 3 09-01-2012 01:57 AM
Different Versions of Python FragInHell Programming 14 05-11-2011 08:26 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration