scripting

All posts tagged scripting

Perl tips

Documentation
A unique feature of Perl is that it doensn’t come with just one man page, but with a whole slew of man pages that describe various aspects of Perl, and serve as tutorials, reference manuals, and FAQ pages. Here are some of the most useful of these pages for beginners:

Line-based operation
The simplest way to use perl in commandline mode is as a filter that operates on a file (or on standard input), manipulates the file one line at a time, and outputs the result to standard output, much like standard Unix utilities like grep, sed, and awk work.

The basic structure of the command is one of the following:

 

The commandline options used here have the following meaning:

The “e” option indicates that the following string is to be interpreted as a perl script (i.e., sequence of commands). To prevent interfering with the shell, it is best to enclose the script in single right quotes (‘).
The “l” (character “ell”, not the bar symbol) option ensures proper end-of-line handling; without it, linebreaks may get chopped off.
The “a” option causes perl to autosplit each line into an array of fields $F[0], $F[1], …, with blank space acting as default field separator. Note that in Perl, array indices start at 0, so the first array element has index 0.
The “p” and “n” options indicate whether or not each line is printed by default.

The following examples illustrate the use of Perl for line-by-line processing of files.

Operating on entire files
Perl’s power really shines when one wants to perform operations on chunks of files that extend over multiple lines (e.g., deleting line breaks in paragraphs). Standard Unix utilities like sed or awk are ill-suited for that, but with Perl this is easy by changing the record separator (which defaults to a linebreak) to something else using the ‘-0’ option. Of particular interest are the following cases:

Here are some examples using these modes:

Cool stuff

 

We have an extremely unstable daemon at work which crashes up to 10 times a day.  It’s completely sporadic, sometimes works great for a full week, then it has days where we have to continually restart it.   We are in the process of upgrading the entire back end to a newer version of the software.  However we need to maintain this until it’s fully upgraded. Here’s a script we used to automatically restart the daemon..

just background the script when starting it.

 

#!/bin/bash
date=date
echo “————————————————-“  >> /root/scripts/checksvc.log
echo “Restarter enabled : $date” >> /root/scripts/checksvc.log
echo “————————————————-“  >> /root/scripts/checksvc.log

while [ 1 ]
do
pidof blahsvc >/dev/null
if [ $? -eq 0 ] ; then
echo “”
date=date
echo “” >> /root/scripts/checksvc.log
else
date=date
hostname=hostname
echo “Restarting BlahSvc : $date” >> /root/scripts/checksvc.log
/etc/init.d/blahsvc start
sleep 10
if [ $? -eq 0 ] ; then
echo “ok!”
else
echo “It didn’t restart.  Failed”
mail sysadmin@example.com -r someone@example.com -s “BlahSvc Startup Failed on $hostname.  Please check!” < /root/scripts/checksvc.log

fi
fi
sleep 30
done