{"id":500,"date":"2007-11-13T20:43:18","date_gmt":"2007-11-13T20:43:18","guid":{"rendered":"http:\/\/neosmart.net\/blog\/2007\/proper-shell-scripting-on-windows-servers-with-perl\/"},"modified":"2013-08-26T18:17:26","modified_gmt":"2013-08-26T23:17:26","slug":"proper-shell-scripting-on-windows-servers-with-perl","status":"publish","type":"post","link":"https:\/\/neosmart.net\/blog\/proper-shell-scripting-on-windows-servers-with-perl\/","title":{"rendered":"Proper Shell Scripting on Windows Servers with Perl"},"content":{"rendered":"<ul>\n<li><strong>Fact:<\/strong> 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. <\/li>\n<li><strong>Fact:<\/strong> Shell scripting on Windows sucks.<sup id=\"rf1-500\"><a href=\"#fn1-500\" title=\"Hopefully Monad (Microsoft Power Shell) will provide a solution, but so far the results are mixed; and it&rsquo;s not popular enough to be considered a viable substitute at the moment.\" rel=\"footnote\">1<\/a><\/sup><\/li>\n<li><strong>Fact:<\/strong> Shell scripting on Linux and other *nix operating systems is powerful, well-documented, and quite straight-forward.<\/li>\n<\/ul>\n<p>Most people take a look at these three facts, and instantly come to a conclusion.. the <em>wrong<\/em> conclusion: you can&#8217;t properly manage a Windows server because it&#8217;s inherently lacking in the shell scripting department.<\/p>\n<p><!--more--><\/p>\n<p>But that&#8217;s not true&#8230; Because here&#8217;s another fact for you:<\/p>\n<p><strong>Perl scripts are a drop-in replacement for 99%<\/strong><sup id=\"rf2-500\"><a href=\"#fn2-500\" title=\"We admit, that&rsquo;s a made up statistic, give us a break though, will ya?\" rel=\"footnote\">2<\/a><\/sup> <strong>of all shell scripting needs.<\/strong><\/p>\n<p>And another fact:<\/p>\n<p><strong>Perl (unlike PHP) runs great (awesome, in fact) on Windows.<\/strong><\/p>\n<p>Now with these two facts in mind, you can now make a proper conclusion: <strong>Shell scripting on Windows doesn&#8217;t have to be difficult, limiting, or in any way inferior than on Linux<\/strong>.<\/p>\n<p>Perl is an awesome language. Between the online Perl community and the millions of Perl-scripting samples across the web, it&#8217;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.<\/p>\n<p>Perl was <em>created<\/em> for stuff like this. It&#8217;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.<\/p>\n<p>With Perl, you can easily do things like manage (prune, grep, or sort) log files, backup <em>and FTP or email<\/em> database server dumps, schedule webserver maintenance, and more.<\/p>\n<p>Shell scripting with Perl is even easier than shell scripting in Bash &#8211; 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&#8217;re purposely refraining from counting lines because this is <em>Perl<\/em> we&#8217;re talking about) to compose an email with your MySQL dumps as a GZIP&#8217;d attachment.<\/p>\n<p>You can easily schedule Perl scripts to run at regular intervals with the Task Scheduler &#8211; but that&#8217;s about all you&#8217;ll ever need to interact with Windows for. <\/p>\n<p>A quick Google search will reveal millions of results for <a href=\"http:\/\/www.google.com\/search?q=perl+server+management+scripts&amp;rls=com.microsoft:en-US&amp;ie=UTF-8&amp;oe=UTF-8&amp;startIndex=&amp;startPage=1\" rel=\"follow\">&#8220;Perl server management scripts.&#8221;<\/a> Take your pick, hack it, and run.<\/p>\n<p>Here&#8217;s our 5-step guide to managing your server with Perl scripts:<\/p>\n<ol>\n<li>Download and install <a href=\"http:\/\/www.activestate.com\/activeperl\" rel=\"follow\">Perl for Windows<\/a> (ActivePerl, free).<\/li>\n<li>Read <a href=\"http:\/\/perldoc.perl.org\/perlfaq.html\" rel=\"follow\">the Perl FAQs<\/a> and <a href=\"http:\/\/en.wikibooks.org\/wiki\/Perl\" rel=\"follow\">get familiar with the syntax<\/a>. It&#8217;s nothing too complicated, and very simple to code in (though reading someone else&#8217;s code is another story). You absolutely don&#8217;t need anything more than the basic syntax, stuff like classes and functions are completely unnecessary for shell scripting &#8211; it&#8217;s too easy to even be considered programming!<\/li>\n<li>Write your Perl script. Use a real text editor like <a href=\"http:\/\/notepad-plus-plus.org\" rel=\"follow\">Notepad++<\/a>.<\/li>\n<li>Test it by running it from the command line and ensuring it does what you need. Repeat step 3 as needed.<\/li>\n<li>Open up Scheduled Tasks in the Control Panel and set up a new task to run your Perl script however often you like.<\/li>\n<\/ol>\n<p>Sure, maybe it&#8217;s not as easy it looks and it&#8217;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&#8217;ll be doing it blindfolded and with <em>both<\/em> your hands behind your back. And it&#8217;ll only be one line long, too! <\/p>\n<p><strong>The most important thing to realize is, you don&#8217;t need to be a programmer (or become one) in order to shell script in Perl<\/strong>.<\/p>\n<p>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&#8217;t need classes, you don&#8217;t need data structures, you don&#8217;t need object orientation, and you don&#8217;t even really need to use variables if you don&#8217;t want to!<\/p>\n<p>We&#8217;ll be posting more details, guides, sample shell scripts, and how-to&#8217;s on Perl-based shell scripting in the weeks and months to come. Don&#8217;t give up just because it involves learning something new, this is in an investment that&#8217;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!<\/p>\n<p class=\"alert\">View <a href=\"http:\/\/neosmart.net\/blog\/tag\/shell-scripting-with-perl\/\" rel=\"follow\">all articles<\/a> in the &#8220;Shell Scripting with Perl&#8221; category.<\/p>\n<hr class=\"footnotes\"><ol class=\"footnotes\"><li id=\"fn1-500\"><p>Hopefully Monad (Microsoft Power Shell) will provide a solution, but so far the results are mixed; and it&#8217;s not popular enough to be considered a viable substitute at the moment.&nbsp;<a href=\"#rf1-500\" class=\"backlink\" title=\"Jump back to footnote 1 in the text.\">&#8617;<\/a><\/p><\/li><li id=\"fn2-500\"><p>We admit, that&#8217;s a made up statistic, give us a break though, will ya?&nbsp;<a href=\"#rf2-500\" class=\"backlink\" title=\"Jump back to footnote 2 in the text.\">&#8617;<\/a><\/p><\/li><\/ol>","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/neosmart.net\/blog\/proper-shell-scripting-on-windows-servers-with-perl\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[568,569,360,5,344,11,566,567,4],"class_list":["post-500","post","type-post","status-publish","format-standard","hentry","category-software","tag-bash","tag-batch","tag-coding","tag-linux","tag-perl","tag-programming","tag-server-management","tag-shell-scripting-with-perl","tag-windows"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4xDa-84","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/500","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/comments?post=500"}],"version-history":[{"count":2,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/500\/revisions"}],"predecessor-version":[{"id":2465,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/500\/revisions\/2465"}],"wp:attachment":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/media?parent=500"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/categories?post=500"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/tags?post=500"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}