- Fact: Shell scripting is a must for any serious IT admin managing a server. From automating backups to checking logs and keeping server performance and load in check, scripting is a must.
- Fact: Shell scripting on Windows sucks.1
- Fact: Shell scripting on Linux and other *nix operating systems is powerful, well-documented, and quite straight-forward.
Most people take a look at these three facts, and instantly come to a conclusion.. the wrong conclusion: you can’t properly manage a Windows server because it’s inherently lacking in the shell scripting department.
But that’s not true… Because here’s another fact for you:
Perl scripts are a drop-in replacement for 99%2 of all shell scripting needs.
And another fact:
Perl (unlike PHP) runs great (awesome, in fact) on Windows.
Now with these two facts in mind, you can now make a proper conclusion: Shell scripting on Windows doesn’t have to be difficult, limiting, or in any way inferior than on Linux.
Perl is an awesome language. Between the online Perl community and the millions of Perl-scripting samples across the web, it’s quite the well-documented language and no sysadmin has an excuse not to use it. The Perl modules are an extensive array of easy-to-use pluggable scripts that you just reference and run.
Perl was created for stuff like this. It’s the language of choice for hacking quick scripts that get the job done, easily, quickly, and with little pain or effort. A Perl script made to run on Linux will likely run on Windows too, with little to no hacking necessary for most of the stuff out there.
With Perl, you can easily do things like manage (prune, grep, or sort) log files, backup and FTP or email database server dumps, schedule webserver maintenance, and more.
Shell scripting with Perl is even easier than shell scripting in Bash – simply because of the huge libraries available that make even the most mundane and PITA tasks quite easy. It takes all of 6 statements (we’re purposely refraining from counting lines because this is Perl we’re talking about) to compose an email with your MySQL dumps as a GZIP’d attachment.
You can easily schedule Perl scripts to run at regular intervals with the Task Scheduler – but that’s about all you’ll ever need to interact with Windows for.
A quick Google search will reveal millions of results for “Perl server management scripts.” Take your pick, hack it, and run.
Here’s our 5-step guide to managing your server with Perl scripts:
- Download and install Perl for Windows (ActivePerl, free).
- Read the Perl FAQs and get familiar with the syntax. It’s nothing too complicated, and very simple to code in (though reading someone else’s code is another story). You absolutely don’t need anything more than the basic syntax, stuff like classes and functions are completely unnecessary for shell scripting – it’s too easy to even be considered programming!
- Write your Perl script. Use a real text editor like Notepad++.
- Test it by running it from the command line and ensuring it does what you need. Repeat step 3 as needed.
- Open up Scheduled Tasks in the Control Panel and set up a new task to run your Perl script however often you like.
Sure, maybe it’s not as easy it looks and it’ll probably take you a day or so to go from absolute zero to cranking your first Perl-based shell script; but soon enough you’ll be doing it blindfolded and with both your hands behind your back. And it’ll only be one line long, too!
The most important thing to realize is, you don’t need to be a programmer (or become one) in order to shell script in Perl.
Just pretend your Perl script is a batch file (yuck!). The Perl processor will run it from top to bottom, in a very straight-forward manner. Put one task/command per-line, stick loops where needed, and test constantly. You don’t need classes, you don’t need data structures, you don’t need object orientation, and you don’t even really need to use variables if you don’t want to!
We’ll be posting more details, guides, sample shell scripts, and how-to’s on Perl-based shell scripting in the weeks and months to come. Don’t give up just because it involves learning something new, this is in an investment that’ll last a lifetime. If you can shell script in Perl on Windows, you can do the same on Linux and OS X with ease. If you can shell script in Perl, you can do anything!
View all articles in the “Shell Scripting with Perl” category.