Shell Script Language - Use Perl, Not Bash
Written by BinnyVA on May 2, 2008 – 11:40 pm -To me, a shell script is a script that automates repetitive tasks. But that is not the ‘official’ definition. Wikipedia has this definition…
A shell script is a script written for the shell, or command line interpreter, of an operating system.
I use a definition that defines the purpose of the script - while the others prefer a definition that defines the technology used. I am not going to claim that my definition is better than the other definition - that’s pointless. Besides, even I think that the ‘other’ definition is the right one. But I will try to show you the advantages of my approach.
The Purpose
I like to automate things(in other words, I’m lazy). So I have a nice little collection of custom shell scripts. But there is a huge barrier to writing shell scripts - the language used. Traditionally shell scripts are written in a language provided by the shell - like bash or tcsh. That is the problem - these languages are Bad - with a capital ‘B’.
The Problem
To people who are accustomed to decent languages, these shell languages will seem clunky - or even evil. But since most shell scripts are small, most people don’t mind the torture.
In bash, the control flow commands seem to be thrown in as a after thought rather than something that’s built into the language. If you don’t believe me, compare the ‘if’ loop of bash and Perl.
Bash
This code checks wether the variable ‘$value’ has the value 0 - if so it prints the message ‘No Value’
if [ $value -eq 0 ] ; then
echo "No Value"
fi
Perl
The same code in Perl…
if($value == 0) {
print "No Value";
}
Of course, Perl experts will go for if(!$value) or even unless($value) - but that’s not the point. See how better the code looks in Perl. Yeah, even I am surprised to hear those words - Perl is considered by many to be an ‘ugly’ language. But when compared to shell languages, Perl is a gem(sorry about the pun - couldn’t resist).
The Solution
The solution is simple - don’t use a shell language to write your shell scripts - use a high level scripting language like Perl. Or Python, Ruby, PHP, Tcl or even JavaScript.
I still use bash to write shell scripts - but if the shell script has an if condition(other than a simple argument check), I use a higher language - usually Perl.
Advantages of using a Shell Language
- Its supported in even the tinest linux distros - even in embedded systems.
- Its the method preferred by the majority - I hope this will change soon.
- Command calls look more natural in a shell language.
Advantages of using a High Level Language
- Better Code
- Easy to maintain
- Faster development
- Libraries provide a lot of functionality
- Easier to port to different platforms.
So why am I telling you all this? Two reasons…
One, the next time you are going to write a shell script, I want you to choose a high level language rather than using bash.
The second reason is that now that my series on Linux MP3 Players are over, I am going to take a small break from desktop posts and write on more ‘linuxy’ topics. And one of those topic is Shell Scripting. So in the future posts, I am going to share some of my shell scripts with you. So when I publish a Perl script and call it a shell script, I don’t want you to get confused .
Digg it
|
reddit
|
StumbleUpon
Tags: bash, Opinion, perl, Programming, script, shell
Posted in Command Line, Programming, Scripting |


Follow me(@binnyva) on Twitter
May 6th, 2008 at 4:16 am
In your example, you seem to be using -eq instead of != just to make Bash seem more foreign.
Unlike perl you generally don’t have to install anything extra on your system for BASH
Is there an easy way to do the following in PERL? Because in BASH doing operations on multiple files at once is very easy.
The below will also work with filenames with strange characters in their name; it will figure out the escape characters for you so passing in the filenames to another program is no trouble.
for x in /home/*; do
echo $x
done
Is there an easy way of telling if a file/directory exists using Perl?
# use -d for directories
if [ -e /tmp/file.txt ]; then
echo “file.txt exists”
fi
PERL is a nice language, but there is no reason to use it over BASH for automated tasks.
May 6th, 2008 at 1:34 pm
Of course there are easy ways:
print “$_\n” for ;
and
print “file.txt exists” if -e /tmp/file.txt;
for easy tasks i also use bash - but with strings, calculations … involved i am much more happy with perl
May 6th, 2008 at 1:38 pm
uups, in the first example my angle brackets disappeared..
it should read
print ā$_\nā for (/home/*);
and now replace the round brackets with angle ones
May 6th, 2008 at 6:11 pm
> Is there an easy way to do the following in PERL? Because in BASH doing operations on multiple files at once is very easy.
> The below will also work with filenames with strange characters in their name; it will figure out the escape characters for you so passing in the filenames to another program is no trouble.
> for x in /home/*; do
> echo $x
> done
In perl, this can be done by
chdir(’/home/binnyva’);
foreach $f (<*>) {
print $f . “\n”;
}
> Is there an easy way of telling if a file/directory exists using Perl?
> # use -d for directories
> if [ -e /tmp/file.txt ]; then
> echo “file.txt exists”
> fi
Sure..
if(-e “/tmp/hello.txt”) {
print “Its there”;
}
> PERL is a nice language, but there is no reason to use it over BASH for automated tasks.
If you are comfortable with bash, there is very little reason to switch over to perl - but most people are not that comfortable with bash. I, for one, prefer using high level languages over bash. But bash has its advantages - as noted in the article.
May 9th, 2008 at 11:39 pm
[...] control or other backup systems built into the tools I use. Anyway, in the first post of the shell scripts series, let me introduce you to my Rsync based local backup [...]
May 12th, 2008 at 11:20 pm
[...] I hate bash. I use perl or other similar high level language to create shell script. I use bash only for the [...]
May 15th, 2008 at 6:45 pm
>I still use bash to write shell scripts - but if the shell script has an if condition(other than a simple argument check), I use a higher language - usually Perl.
I agree with you about using a higher language for more complicated scripts. Regarding the language, I prefer Python.
May 15th, 2008 at 11:22 pm
@nongeekboy
Python, Perl - it does’nt matter. I say tomato, you say tomoto(yeah, the joke is totally lost when written).
As long as its not bash, I’m happy.
May 19th, 2008 at 9:09 pm
Will: In the shell, using -eq forces a numeric comparison v/s using ==, which does a string comparison. It’s better to use the -eq/-ne versions in the shell when comparing numbers. It’s slightly faster, and will generate a warning if non-numeric input is presented.
To counter the argument that perl looks better, consider a case statement in the shell - that has to be a big if/elsif/else block in perl, since perl has no case statement (yeah, there’s a non-standard module which sortof adds a case-like structure). There’s no perl equivalent which looks better than this shell code.
case $var in
1 ) print “var is one” ;;
2 ) print “var is two” ;;
* ) print “var is not one or two” ;;
esac
In any event…
I use the shell (ksh, usually, but Bash is fine) for operations which involve a lot of system calls (automating processes) or looking at filenames. I use perl if I’m doing more than a quick grep on files or anything more involved - though one can do some pretty complicated things if one looks into the shell’s more advanced capabilities. They’re two different tools, and each has an area in which they’re the better choice. Much as I love perl (and I was a proessional perl developer for a while), the shell has its niche.
June 2nd, 2008 at 4:25 pm
I never tried perl for shell script before but now I find it to be useful for some instances and I agree with Danny that it has it’s niche.
June 5th, 2008 at 1:36 am
Hi Binny, thanks for the post — very imformative.
As part of my career advancement with my current testing of software applications that sit on top of linux, I am learning scripting using Python — b/c that is the High Level Language the Lead Software developer on our team evangelizes.
If it is worth you writing a post that compares the major High Level Languages, then I would love to see it.
Sounds like, in the future, I might also desire to then learn some scripting in straight bash.
My sitting in the lab for one-hour-plus, manually grepping for “errors” on log file results from an overnight test, is getting boring.
“I’m feeling the pain, so I’m seeing the light.”
June 6th, 2008 at 9:22 am
this article is very very informative ..
hats off to Binny..good work guy…
could u please answer my simple query
Ques -1>>what are all the scenarios under which perl is useful for software testing??
Since i am a Sr. Software Test Engineer and planning to explore this world if it may helpful in carrying my testing Task.
So your expert suggestion is solicted?
June 6th, 2008 at 12:22 pm
@RahulKumar
Sorry - but I am not that experienced in the testing field. So I cannot answer that question.
June 7th, 2008 at 10:20 am
@Binny
Not a problem Binney…I have one more question..
Suppose i have couple of file in my c:\ drive and i want to replace TRASECTION to CARD_TRANSECTION to give better visibility in around 50 file.So i have perl script habdy.
But how i run this..in Window platform..
Could we execute this script on Windows Platform after installing PERL softwate..Since i though i will install trial version of the same.
or
We cant execute perl script on windows platform?
or
Could we install PERL on Windows platform?
I know this all are my silly question ,But if you could answer these
it i would be very gteatful to you my brother..
You could send your answer rahulkumarbangalore@yahoo.com
Regards,
Rahul
June 7th, 2008 at 11:11 am
@RahulKumar
You can easily install perl in Windows - just go to the Perl site - they have a installer for windows.
June 7th, 2008 at 1:49 pm
ok i am downloading Windows installer —http://www.activestate.com/store/download_file.aspx?binGUID=c1b8de46-32d8-440c-a17a-80eb31e68903
from above site.But it says SYSTEM REQUIREMENT as below
1>x86 or x64 architecture
2>Windows 2000, XP or Vista: No additional requirements.
3>Perl for ISAPI: ISAPI compatible Web server such as IIS 4.0+ or PWS 4.0+
4>PerlScript: ActiveX scripting host such as IE 4.0+, or Windows Scripting Host
RQUIRMT1> I alredy have
RQUIRMT2> I have winXP
RQUIRMT3>wt is this.how to full fill this
RQUIRMT4>is this requirement talking
about IE version if so i have
IF7.What is windows scripting
HOST?
Could you please give some more idea what i need to do to satisfy requirement 3 and 4?
If i am not wrong RQUIRMT3 talks about some web server..Could we download this from Microsoft? if yes i could google it and install the same..
One more question why this is really require..i still didnt get..i know for my application it is not require since i need to replace string with new string..
But i might guess it could be useful somrwhere..So could you let me know where it could play a role?
Thanks & Regards,
Rahul
June 9th, 2008 at 10:19 am
Hi Binny
Any updates would really appreciated..
June 10th, 2008 at 5:12 pm
hi,
i need small favour from this forum.
i need to search a string into a file and i need to replace it with new string.
but i am unable to do so.i hve done some home work as well.i am pasting down…
$data_file=”aaa.txt”;
#appending
open (handler,”>>$data_file”) || die(”coudnt open”);
@raw_data=;
for ($i=o ;$i<4 ;$i++)
if (@raw_data[i] eq “old_string”)#if matched is found
wt should i write here to replace??
i am executing this perl script on Window through Command prompt
end if
close (handler)
Regards,
Lax!!
June 10th, 2008 at 5:14 pm
raw_data array is assigned to handler.
i.e @raw_data=handler;
July 16th, 2008 at 5:08 pm
you may be able to use something like:
#!/bin/perl -w
#
use strict;
my $search_string=”this”;
my $replace_string=”that”;
while() {
$_ =~ s/$search_string/$replace_string/ig;
print $_;
}
or from a command line you could just run it as :
$:> cat aaa.txt | perl -ne ‘$_ =~ s/This/that/ig; print ‘;
July 30th, 2008 at 2:25 am
Hello,
I have a question. I use shell script (bash, ksh, etc.) to automate a lot of things like grouping all OS commands together and run them in a script. For example, in an Oracle RAC environment running on Linux, I use srvctl extensively for adding and removing services, etc. If I use perl, how can I do this? Please give examples as I do not know Perl well.
Regards,
July 30th, 2008 at 10:28 am
@Tom
Just put those command in backticks(`). Like this
`service stop mysqld`
July 30th, 2008 at 7:02 pm
Thanks BinnyVA.
July 31st, 2008 at 10:38 am
could any one do a favour for me.. i want a bash or python or perl script to run more than one Bash scripts… please help me out…
December 18th, 2008 at 11:46 pm
Nice explanation.
http://unstableme.blogspot.com/search/label/Bash
January 21st, 2009 at 11:41 pm
[...] to love the features that is provided by bash(Linux shell scripting language). Then after a while, you will hate that too - and will use higher level languages. And thus, a programmer is [...]
February 7th, 2009 at 2:22 am
Every unix sysadmin should be well versed in shell scripting. It’s mandatory. For that same group of people, perl is not mandatory, but a very nice to have. Take a look at the OS init scripts. They’re all shell scripts. These are the kinds of scripts sysadmins work with the great majority of the time. Yes, there are times when perl makes more sense, but I would say this is rare…for the sysadmin crowd. If you’re a sysadmin, look at any decent init script. If you can’t write something similar…with the same level of difficulty/elegance, you need to improve your shell scripting skills. Another aspect to keep in mind is shell scripting is basically just blocks of system commands. As a sysadmin, the better you can shell script, the better you are at the command line and vice versa. A sysadmin spends most of his time operating on the commandline. You might as well write as much of your scripts in bash as possible to complement your commandline skills.
February 7th, 2009 at 2:33 am
@Vu
Good point. I am not a sysadmin - I am a programmer. So my thinking might be a bit biased in that direction.