Perl code to convert from CSH to BASH??

Discussion in 'Perl Misc' started by Ahmad, Jan 27, 2008.

  1. Ahmad

    Ahmad Guest

    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.
    Ahmad, Jan 27, 2008
    #1
    1. Advertising

  2. Ahmad

    Henry Law Guest

    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
    Henry Law, Jan 27, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. thewhizkid

    @INC equiv in .csh scripts ?

    thewhizkid, Jan 21, 2004, in forum: Perl
    Replies:
    1
    Views:
    1,154
    Michael Korte
    Jan 21, 2004
  2. William Park

    (patch for Bash) GTK+2 + Bash

    William Park, Jul 11, 2005, in forum: XML
    Replies:
    3
    Views:
    2,295
    William Park
    Jul 14, 2005
  3. Nader Emami

    csh to Python

    Nader Emami, Jan 4, 2005, in forum: Python
    Replies:
    1
    Views:
    626
    Max M
    Jan 4, 2005
  4. David Hirschfield

    Parsing csh scripts with python

    David Hirschfield, Mar 28, 2006, in forum: Python
    Replies:
    2
    Views:
    327
    gene tani
    Mar 29, 2006
  5. Ilya Zakharevich

    Detecting Bourne (or csh) shell from Perl

    Ilya Zakharevich, Mar 3, 2010, in forum: Perl Misc
    Replies:
    8
    Views:
    194
    Ilya Zakharevich
    Mar 10, 2010
Loading...

Share This Page