LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 02-04-2008, 10:32 PM   #1
DJOtaku
Member
 
Registered: Oct 2004
Location: USA
Distribution: Fedora 20; Arch; Kubuntu; Debian
Posts: 711

Rep: Reputation: 31
bash vs perl vs python


I've written my fair share of bash scripts and I've written a few python scripts/programs.

I was wondering, when is perl better than bash? So far I haven't encountered much that bash couldn't handle. The only reason for my python scripts/programs have been to learn that language, not necessarily because bash couldn't do the same thing.

And between perl and python, when is it better to use one over the other? Or are they pretty much equivalent?

Thanks,
 
Old 02-04-2008, 11:32 PM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
Well, bash is intepreted line by line as it runs and depends on calling a lot of external progs (depending on what you want to do).
You often have to use temp files as intermediate storage for result sets.
It (shell) was originally designed to talk to the system and automate cmd sequences (shell files).

Perl is more like C, it's largely self contained with a huge library of free code (search.cpan.org) and it's compiled (sort of see http://www.perl.com/doc/FMTEYEWTK/comp-vs-interp.html ), so it runs much faster, eg about 80-90% speed of C, but easier to program (eg variable sizes are dynamic).
Syntax is similar to C. Docs: http://perldoc.perl.org/

I'll pass on Python, never seen/used it.

HTH
 
Old 02-04-2008, 11:34 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by DJOtaku View Post
I've written my fair share of bash scripts and I've written a few python scripts/programs.

I was wondering, when is perl better than bash?
It is better than bash only when you think it is.

Quote:
And between perl and python, when is it better to use one over the other? Or are they pretty much equivalent?
Thanks,
i would rather you not ask this question here because it will most definitely lead to unnecessary "arguments". The best solution is to find that out for yourself. Learn Perl and Python. Then do up a piece of application using both languages to reach your own conclusion.
 
Old 02-04-2008, 11:53 PM   #4
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,458

Rep: Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172
My general take...

(1) Although bash has rudimentary scripting capabilities, it is primarily meant to be "a shell." You can do a lot of things with its scripting-language, if you work at it hard enough, but that isn't what this tool is really built for.

(2) General-purpose programming languages such as Perl and Python, and to a slightly lesser extent PHP and Ruby, are tools that are "built for" general-purpose programming. Any of these can be invoked from the shell, and you can write a good command-program in any of them.

(3) Also don't overlook some lighter-weight but nevertheless very purpose-driven tools such as 'awk.' Unix/Linux environments give you a very rich library of "tools for the job."

(4) Make it your business ... it can also be informative and fun ... to study, and over time to master, as many tools and languages as you can. In other words, don't waste time quibbling over "Perl vs. Python." Instead, learn both!

(If anyone swaggers up to the bit-bar spoiling for a fight over which one or the other is "best," tip your hat, wish them a very nice day and leave the bar immediately. "Flameboys" aren't even any good for toasting marshmallows.)
 
Old 02-05-2008, 12:44 AM   #5
DJOtaku
Member
 
Registered: Oct 2004
Location: USA
Distribution: Fedora 20; Arch; Kubuntu; Debian
Posts: 711

Original Poster
Rep: Reputation: 31
I guess it just comes down to me not yet having any programs "un-trivial" enough to warrant writing in perl rather than bash script. For example:

Here are just a few examples of some my bash scripts.

This one I use to create animations from the PNG files created in Blender. (Without audio - more for test renders)
Code:
#!/bin/bash

# Written by Eric Mesa
# requires mencoder
# Version 0.1 - created the script

#ask the user a series of questions to fill in the fields below:

echo "what is fps?"
read fps

echo "what bitrate? [recommend 3000 for smaller animations]"
read bitrate

echo "what is the title?"
read title

echo "what is genre?"
read genre

echo "what is output name?"
read outputname

mencoder "mf://*.png" -mf fps=$fps -ovc xvid -xvidencopts bitrate=$bitrate -info name="$title":artist="Eric Mesa/drop the bomb productions":copyright="2007 creative commons by-nc-sa":genre="$genre" -o $outputname.avi
So, could this be improved by converting to perl or is it too simplistic?

Most of my scripts are like that one, for example:

Code:
#!/bin/bash

#Written by Eric Mesa
#requires ffmpeg
#original version copied from http://www.jochem.name/DVD-Authoring-with-Linux
#questions added by Eric

echo "what is path to file?"
read orig

echo "what is ouptut filename?"
read output_dvd

ffmpeg -i $orig -target dvd -aspect 4:3 $output_dvd.mpg
#where in the next part n is the pass number
#ffmpeg -i input.avi -target dvd -aspect 4:3 -hq -passlogfile logfile.log -pass n output.mpg

tcextract -i $output_dvd.mpg -x mpeg2 > output.m2v

tcextract -i $orig -x ac3 -a 0 > output.wav

mplex -f8 -o $output_dvd.mpg ouput.m2v output.wav
My most complex one was written by a friend of mine to help me backup my home directory:
Code:
#!/bin/bash

SNAPDIR=/media/usbdisk/home_backup
#oldest snapshot to be deleted (4 means there are 4 kept)
B30="`ls -rd ${SNAPDIR}/snapshot* | awk -F' ' '{print NR,$1}' | awk -F' ' '{if($1>4)print$2}'`"
B0=${SNAPDIR}/snapshot.`date +%Y%m%d%H%M%S`

wall "about to begin rsync backup"
echo "Starting backup `date`"

echo "Starting rsync..."
rsync -avz --delete /home/e/ ${SNAPDIR}/snapshot.current

echo "Rotating Backups..."
#created hardlinks so there don't have to be multiply copies of the files around
cp -al ${SNAPDIR}/snapshot.current ${B0}
rm -fr ${B30}

echo "Finished Backup 'date'"
wall "rsync backup done"
would that one benefit from perl?

---

But in the end, yes, I've learned Python - was doing some wxPython coding tonight. And I've learned some rudimentary Perl. I just can't figure out when to use it over bash or python. In order words, I didn't want to start a flame war, but rather I'd like to know the strengths of each and use each where it's the strongest.

Last edited by DJOtaku; 02-05-2008 at 12:46 AM. Reason: adding conclusion
 
Old 02-05-2008, 01:20 AM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
Well, as mentioned, for automation (like you've done) bash is prob sufficient, although I'd reccommend checking exit status' ie
Code:
do_cmd
if [[ $? -ne 0 ]]
then
    do_error_handling
fi
especially for important stuff like backups

Perl/Python are more for actual data processing (like C) or where your script is non-trivial in length/complexity (for some value of ...), especially the latter.
I'm pretty sure you can find a dozen other threads on this sort of qn 'best lang..' if you search around.
Use the best lang for a given task, or the one you are most comfortable with if several options are available.
(If you need/would like a pre-written module to help in a given task, I believe search.cpan.org ie perl modules is one of the largest around.)
YMMV as they say....
 
Old 02-05-2008, 01:57 AM   #7
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
IMHO, bash is best for smaller scripts. AWK is great for handling structured data files.
Bash however only has one dimensional arrays and doesn't support floats or data structures. You can use functions but if you need more structure in the program, or if your program is large or speed is important, look at something else. Remember that for a larger program, debugging support will become more important.

On my system at least, there are python scripts used with the hal system. HPLIP also uses Python scripts. When I used Mandrake Linux, its administration scripts tended to be written in PERL. SuSE's tend to be written in bash.

The blender3d program can use Python plug-ins. Amarok uses ruby plug-ins. You wouldn't run a website in bash. You might in Perl, and some large websites do. Plone uses Python. So one criteria could be what the other's are written in in the context of what you are doing. If you are writing a service wrapper to in /etc/init.d/ you would base it on the skeleton script, be it bash or perl.
 
Old 02-05-2008, 02:45 AM   #8
paddy3118
LQ Newbie
 
Registered: Jul 2007
Location: Bristol UK
Distribution: RedHat, Cygwin
Posts: 14

Rep: Reputation: 0
DJ,
Bash is a quite capable scripting language, as you have found, but, If you start using arithmatic, using bash arrays, using array variable modifiers,... then it is usually time to put down bash and switch to a more capable language such as Python - my choice, and I have to know and use Python, Perl, TCL, Awk, and Bash.

Here's a large Bash script I wrote in Bash because I new a lot more about background job management in Bash - but, on reflection, might have been better written in Python because of later additions -
http://paddy3118.blogspot.com/2007/1...ash-shell.html

And here's why I limit my use of Perl:
http://paddy3118.blogspot.com/2006/0...with-perl.html

- Paddy.
 
Old 02-05-2008, 03:11 AM   #9
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Rep: Reputation: 32
Smile

I tend to use php for most of the stuff.. It really is easy because of the enormous support you get with it. It has support for many many libraries (including curl), there is pear.php.net etc.. No matter what you are trying to do, there already is a function or class for it. Don't know about the performance though.. Some say python is better in that field. Don't really know Cheers
 
Old 02-05-2008, 04:40 AM   #10
Zmyrgel
Senior Member
 
Registered: Dec 2005
Location: Finland
Distribution: Slackware, CentOS, RHEL, OpenBSD
Posts: 1,006

Rep: Reputation: 36
I really dislike Perl. "Perls like C"... Main reason I don't like Perl is that it's not like C

What really stopped me from learning more Perl was it's attitude that there's no proper way to do things, the whole "there's more than one way to do it" -attitude. Describing a way to do switch case structure for 2 pages was where I drew the line.
Why not to stick with one way that works?

Python, though, has kept my interest up since I started learning it. Everything in the language seems a logical.

I think both of them can achieve pretty much same stuff.
 
Old 02-05-2008, 05:55 PM   #11
DJOtaku
Member
 
Registered: Oct 2004
Location: USA
Distribution: Fedora 20; Arch; Kubuntu; Debian
Posts: 711

Original Poster
Rep: Reputation: 31
Thanks for that! I think I'm much better prepared to make a decision about it.
 
Old 02-07-2008, 07:21 AM   #12
Su-Shee
Member
 
Registered: Sep 2007
Location: Berlin
Distribution: Slackware
Posts: 509

Rep: Reputation: 41
I use bash, if it's just a short one or two liner or I just throw it on the command line.

I use Perl for literally everything else, especially when it comes to _any_ kind of string and character handling and regular expressions which are perl's strengths - many concepts from AWK are nicely incorporated into perl and sugar-coated with additional features. I also use Perl if I need networking stuff or some GUI added.

I use Python, if I _really_ need "real" object orientation and Perl's blessed hash-OO isn't enough. As I'm starting to experiment with Perl 6, I may have no need for Python in the future.

Another choice might be Ruby - I've got no desire or need for it personally, but just to mention it...

Personally, Perl's still my favorite for many reasons starting with "community and spirit" over "CPAN" and by far not ending with "extremely well documented".
 
  


Reply

Tags
bash, perl, python, script, scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 05:11 AM
Perl and Python raghuveerbabu Programming 10 08-24-2007 09:45 AM
bash vs perl vs python keceli Programming 11 07-23-2007 11:57 PM
bash, perl, or python? s_siouris Linux - Software 11 05-13-2005 04:15 PM
Perl or Python ! linuxlover1 Programming 13 04-19-2004 08:33 AM


All times are GMT -5. The time now is 08:55 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration