in reply to Re: Need help to convert short c shell script to perl
in thread Need help to convert short c shell script to perl

Not quite true. Here is an experiment I did:

Given a file to be sourced in like this:

VAR1="FOO" VAR2="FEE" VAR3="FUM" export VAR1 export VAR2 export VAR3
Here is how I sourced it in:
#!/usr/bin/perl -w use strict; use Data::Dumper; my %envstuff=source_in("source.sh"); print Dumper(\%envstuff); sub source_in { my $fname=shift; my @vars=(); open RIPIT,sprintf("(. $fname ; env) | ",$fname) or die $fname . $ +!; while(my $line=<RIPIT>){ chomp $line; my ($key,$val)=split("=",$line); push @vars,$key,$val; } close RIPIT; return @vars; }
It ain't pretty but it works. Here is a truncated version of the results:
$VAR1 = { 'VAR1' => 'FOO', 'VAR2' => 'FEE', 'VAR3' => 'FUM',
I'm sure there is more than one way to get this done and I'll be some of them are quite elegant, but sometimes I like to just pull a hammer out of the toolbox and whack nails that stick out.


Peter L. Berghold -- Unix Professional
Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

Replies are listed 'Best First'.
Re^3: Need help to convert short c shell script to perl
by Tux (Canon) on Sep 25, 2007 at 15:30 UTC

    Very unlikely to be the OP's input format. You're assuming sh syntax, whereas the OP's code is csh syntax, and hence his script would more be like

    setenv InVAR0 42 setenv InVAR1 24

    and that would be easy to parse, but what if it was

    set foo=`foo` setenv bar "$foo":$PATH":"`cat /etc/PATH`

    or even worse

    set foo=`ttytype` eval $foo

    Enjoy the puzzle

    BTW I am a tcsh user myself, but I fully agree with TomC that csh/tcsh is not something to script in. Ever!


    Enjoy, Have FUN! H.Merijn
          You're assuming sh syntax, whereas the Op's code is csh syntax,

      Actually not assuming. I just defaulted to what I run into the most myself. Hardly ever touch CSH these days. I was more interested in demonstrating a concept than anything else.


      Peter L. Berghold -- Unix Professional
      Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
        I have tried to write a ksh to perl convertor RFC: UNIX shell to Perl converter, and csh has simpler syntax (for a parser, not a user).
        I gave up because I found most shell scripts are written very badly, and bad shell scripts converted into really bad perl.