[Linux-users] converting bash scripts to perl
Bill Campbell
linux-sxs
Tue Aug 28 10:34:28 PDT 2007
On Sat, Aug 25, 2007, Lonni J Friedman wrote:
>Anyone have any experience converting a large number of bash shell
>scripts over to perl?
The attached script might be used as a first cut. I wrote it
years ago to parse simple shell scripts to perl, mostly scripts
which set environment variables.
It might be easier to convert to python as simple assignment
statments will work in a large percentage of the cases.
This script will require a bit of cleanup as it's calling the
OpenPKG instance of perl on my mail server.
Bill
--
INTERNET: bill at celestial.com Bill Campbell; Celestial Software LLC
URL: http://www.celestial.com/ PO Box 820; 6641 E. Mercer Way
FAX: (206) 232-9186 Mercer Island, WA 98040-0820; (206) 236-1676
The difference between science and the fuzzy subjects is that science
requires reasoning while those other subjects merely require scholarship.
-- Robert Heinlein
-------------- next part --------------
:
#!/csrel25/bin/perl
eval ' exec /csrel25/bin/perl -S $0 "$@" '
if $running_under_some_shell;
shift if(join("", @ARGV) eq ""); # Xenix Kludge
# $Header: /vol/cscvs/lbin/shtoperl,v 1.6 1995/12/24 01:35:56 bill Exp $
# $Date: 1995/12/24 01:35:56 $
# @(#) $Id: shtoperl,v 1.6 1995/12/24 01:35:56 bill Exp $
( $progname = $0 ) =~ s!.*/!!; # save this very early
$USAGE = "
#
# Usage: $progname [-v] [-d dirctory] file [file...]
#
# Options Argument Description
# -s Strip comments
# -v Verbose
#
";
sub usage {
die join("\n", at _) .
"\n$USAGE\n";
}
do "getopts.pl";
do "/csrel25/etc/csspath.perl" unless $usr_bin_csspath;
&usage("Invalid Option") unless do Getopts("sv");
$verbose = ( $opt_v ? '-v' : () );
$\ = "\n"; # use newlines as separators.
$double_quotes = '""';
$single_quote = '"';
sub shtoperl {
local($file_handle) = @_;
open($file_handle, $file_handle);
while(<$file_handle>) {
next if /^:/;
chop;
if(/^[\s;]*#/) {
print unless $opt_s;
next;
}
foreach (split(/;\s/, $_, 9999)) {
next if ( /^\s*set\s+-x/ );
if (/^\s*\.\s*(\S+)/) {
print "# including $1";
&shtoperl($1);
next;
}
$comment = '';
if(/#/) {
($comment = $_) =~ s/[^#]*(\s*#.*)/$1/;
($pattern = $comment) =~ s/([()])/\\$1/g;
s/$pattern$//e if $comment; # strip trailing comment
s/(\s*)$//; # trailing white space
$comment = "$1$comment";
}
next if ( $opt_s && /^\s*$/ );
;# take care of -a in if statements.
s/\s+-a\s+/ && /g;
if(/^(\s*)\[(.*)\]\s+&&\s+\{/) #}
{
$leadspace = $1;
$_ = $2;
s/-z/!/;
s/\s+=\s+/ eq /;
s/\s+!=\s+/ ne /;
s/""/''/g;
# s/"//g;
# s/\$(\S+)/\$ENV{"$1"}/g;
print "${leadspace}if ($_) \{"; #}
next;
}
s/;$//;
s/(\s*)([^=]*)=(.*)/$1\$$2 = "$3";/;
s/(\s*)export\s*(.*)/$1\$ENV{'$2'} = \$$2;/;
s/$double_quotes/$single_quote/g;
s/"''"/''/g;
s/^(\s*)if\s+test(.*)/$1if( $2 )/;
s/^(\s*)if\s+-z/$1if !/;
s/^(\s*)then/$1\{/;
s/^(\s*)fi/$1}/;
# the next two lines take care of backtick operators.
s/"*`"*/`/g;
s/(\S+\s+\=\s+\S*\`.*`)/chop($1)/g;
s/^(\s*)unset\s+(\S+)/$1delete \$ENV{'$2'};/;
print "$_$comment";
}
}
close($file_handle);
}
for (@ARGV) {
&shtoperl($_);
}
More information about the Linux-users
mailing list