3

I had a bash-backup-script for quite some time which worked ok for me, but now I needed some features and put them into a perl-script. It works perfectly fine, and I'm happy. But my muscle-memory always wants to type "sh backup" instead of "perl backup" (or using "./backup" or something like that). I happened to start with "sh backup" and now I can barely get rid of that anymore.

My idea was this:

Having a module cleverfilter.pm:

package cleverfilter;
use Filter::Simple;
FILTER {
    s/perl backup; exit;//g;
};
1;

and this in my backup-script:

#!/usr/bin/perl

use cleverfilter;

perl backup; exit;

use strict;
use warnings;
...

The idea behind that was that a perl-script would (through the filter) remove the "perl backup; exit;"-part, but bash wouldn't. Perl would run fine, and bash would run this script as a perl-script.

But Filter::Simple seems to have one problem: it filters everything after it's modules use. That's why I needed to put it on top of the "perl backup"-line. Now, when I run it with perl, it works like a charm. Running it with bash, obviously, gives me these problems:

backup: 3: backup: use: not found

Of course, I could just create an empty program "use" somewhere that ignores all the parameters coming, but that's not a nice solution to this.

So, my question here: is there any way to write some script-part that will be executed with bash, but not with perl? Some kind of comment, e.g., like in HTML with those weird IE-Style-Comment-Tags?

Or is there any better solution? Something easier I haven't thought of yet?

4

One option would be to use the perl -x option like this:

#!/bin/bash
perl -x "$0"
exit
#!/bin/perl
#perl code goes here

On my system that works for either sh or perl.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.