Click here to get back home

Perl code to convert from CSH to BASH??

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Perl code to convert from CSH to BASH?? Ahmad 01-27-2008
Get Chitika Premium
Posted by Ahmad on January 27, 2008, 3:56 am
Please log in for more thread options
Hi,

I was trying to write a Perl code to be used to convert CSH scripts to
BASH. I target to translate .cshrc file to .bashrc one.

My question is: In converting "path" setting statement i was trying
to use "join" and "split" functions.

For example:

set path = (/bin /usr/bin /sbin $HOME/bin ) ===> export PATH=$PATH:/
bin:/usr/bin:/sbin:$HOME/bin

I wrote the part of Perl code like that ($a is holding the line to be
converted):

$a=~s/[()]//g ; # To remove parentheses
$a=~s/set\s+path\s+=(.+)/export PATH=$PATH:/g;
$b=join(':',split(/\s+/,$1);
$a.=$b;
print FID $a;

And it worked fine, but as you see, the code is very long to convert a
very small part.. Is it possible to include and evaluate function
within the RE replacement side?

Something like that: $a=s/....../....FUNCTION TO BE EVALUATED (e.g.
join.....)..../eg;

How can i do it?

Thanks a lot in advance,
Regards,
Ahmad

P.S. If anyone knows an easiest way to convert from csh to bash, or
had already written a script that does that, please tell me about it..
Thanks a lot.

Posted by Henry Law on January 27, 2008, 11:07 am
Please log in for more thread options
Ahmad wrote:
> For example:
>
> set path = (/bin /usr/bin /sbin $HOME/bin ) ===> export PATH=$PATH:/
> bin:/usr/bin:/sbin:$HOME/bin
>
> I wrote the part of Perl code like that ($a is holding the line to be
> converted):

Don't do that. (1) Better to use variables that mean something; (2) $a
and $b are used in the "sort" statement and are, by convention, reserved
for that use.

<snipped wordy code ...>

> How can i do it?

Having not a lot better to do this afternoon I had a look at this.
Remember that converting the "set path" statement isn't the only thing
you need to. I don't know csh but I know at least that you'll need to
convert the shebang, and probably a whole lot of other lines as well.
So you're really writing a loop that reads in the csh line by line and
converts each one as appropriate. Here's a starter; it does the "export
path" statement as you requested, and also the shebang. You can add
more "elsif" sections for the other things you need to convert.

#! /usr/bin/perl
use strict;
use warnings;
while ( my $line = <DATA> ) {

if ( $line =~ /^set\s+path\s?=\s?\((.*)\)/ ) {
# Process lines containing set path = ( /some/stuff )
# (You could refine the pattern inside the capturing brackets;
# something like [\w\s/$] comes to mind)
my @path_elements = split /\s+/,$1;
print "export PATH=$PATH:", join( ':', @path_elements ), "\n";

} elsif ( $line =~ m|^\#!/usr/bin/csh| ) {
# Process the shebang line
print "\#!/usr/bin/bash\n";

} else {
print $line;
}

}
__DATA__
#!/usr/bin/csh
#
set path = (/bin /usr/bin /sbin $HOME/bin )

--

Henry Law Manchester, England

Similar ThreadsPosted
how to convert the following ASP code to perl code May 6, 2007, 8:35 am
convert Vbscript to Perl code January 15, 2007, 8:31 am
bash and perl February 17, 2008, 8:49 pm
with bash : hostname -d - with perl? May 24, 2006, 9:55 pm
is there a bash script to perl converter? November 17, 2006, 11:48 pm
Is it possible to embed perl inside a shell script say bash? September 23, 2004, 8:15 pm
getting bash errors March 15, 2007, 4:03 pm
Code Quality and examples from the Perl source code April 11, 2006, 10:22 am
Separate Javascipt code from pure Perl code May 7, 2007, 3:58 am
Re: Translate BASH source July 16, 2008, 1:10 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap