Help: How to change PATH variable by array?

Discussion in 'Perl Misc' started by Amy Lee, Aug 26, 2007.

  1. Amy Lee

    Amy Lee Guest

    Hi,

    I make a Perl script to add a path in PATH variable of the .bash_profile.
    Just like

    PATH=/usr/java/jdk1.5.0_09/bin:$HOME/perl:$HOME/shell:$PATH:$HOME/bin

    to be

    PATH=/usr/local/vice:/usr/java/jdk1.5.0_09/bin:$HOME/perl:$HOME/shell:$PATH:$HOME/bin

    There's my code:
    $ADD="/usr/local/vice"
    foreach $FILE (@ARGV)
    {
    open $IN, '<', $FILE;
    while (<$IN>)
    {
    if (/PATH=/)
    {
    chomp;
    s/PATH=/;
    my @PATH=s/:/ /;
    unshift @PATH, $ADD;
    print $_;
    }
    }
    close $IN;
    }

    But when I run this script, it still displays

    /usr/java/jdk1.5.0_09/bin $HOME/perl $HOME/shell $PATH $HOME/bin

    Could you tell me how to solve this problem?

    Thank very much~

    Regards,

    Amy Lee
     
    Amy Lee, Aug 26, 2007
    #1
    1. Advertising

  2. Amy Lee <> wrote:


    > I make a Perl script to add a path in PATH variable of the .bash_profile.
    > Just like
    >
    > PATH=/usr/java/jdk1.5.0_09/bin:$HOME/perl:$HOME/shell:$PATH:$HOME/bin
    >
    > to be
    >
    > PATH=/usr/local/vice:/usr/java/jdk1.5.0_09/bin:$HOME/perl:$HOME/shell:$PATH:$HOME/bin



    # untested
    perl -p -i.bak -e 's#PATH=#PATH=/usr/local/vice:#' .bash_profile


    > There's my code:



    If you say so.


    > $ADD="/usr/local/vice"



    Syntax error. No semicolon.


    > foreach $FILE (@ARGV)
    > {
    > open $IN, '<', $FILE;



    You should always, yes *always*, check the return value from open():

    open $IN, '<', $FILE or die "coulde not opne '$FILE' $!";


    > while (<$IN>)
    > {
    > if (/PATH=/)
    > {
    > chomp;
    > s/PATH=/;



    Another syntax error. There are 2 parts to a s/// operator.

    This clearly is NOT your code.


    > my @PATH=s/:/ /;



    @PATH now contains 1 element, whose value is 1.

    I doubt that that is what you want. Maybe you wanted to split
    on colons instead:


    my @PATH = split /:/;


    > unshift @PATH, $ADD;
    > print $_;
    > }
    > }
    > close $IN;
    > }
    >
    > But when I run this script, it still displays
    >
    > /usr/java/jdk1.5.0_09/bin $HOME/perl $HOME/shell $PATH $HOME/bin
    >
    > Could you tell me how to solve this problem?



    Start by showing us the code that you really have.

    Have you seen the Posting Guidelines that are posted here frequently?


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad McClellan, Aug 26, 2007
    #2
    1. Advertising

  3. Amy Lee

    Mumia W. Guest

    On 08/26/2007 08:08 AM, Amy Lee wrote:
    > Hi,
    >
    > I make a Perl script to add a path in PATH variable of the .bash_profile.
    > Just like
    >
    > PATH=/usr/java/jdk1.5.0_09/bin:$HOME/perl:$HOME/shell:$PATH:$HOME/bin
    >
    > to be
    >
    > PATH=/usr/local/vice:/usr/java/jdk1.5.0_09/bin:$HOME/perl:$HOME/shell:$PATH:$HOME/bin
    >
    > There's my code:
    > $ADD="/usr/local/vice"
    > foreach $FILE (@ARGV)
    > {
    > open $IN, '<', $FILE;
    > while (<$IN>)
    > {
    > if (/PATH=/)
    > {
    > chomp;
    > s/PATH=/;


    This does not compile. This is not your real program. We cannot debug an
    imaginary program.

    > my @PATH=s/:/ /;


    Why are you using the s/// operator in place of the split() function?

    > unshift @PATH, $ADD;
    > print $_;
    > }
    > }
    > close $IN;
    > }
    >
    > But when I run this script, it still displays
    >
    > /usr/java/jdk1.5.0_09/bin $HOME/perl $HOME/shell $PATH $HOME/bin
    >
    > Could you tell me how to solve this problem?
    >
    > Thank very much~
    >
    > Regards,
    >
    > Amy Lee


    Technically, this looks like it should be a one-liner, but here is a
    three-liner.

    use strict;
    use warnings;

    while (<>) {
    s/(?<=^PATH=)/\/usr\/local\/miami\/vice:/;
    }

    __UNTESTED__
     
    Mumia W., Aug 26, 2007
    #3
    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. Baby Lion
    Replies:
    2
    Views:
    400
    Baby Lion
    Oct 2, 2006
  2. mfglinux
    Replies:
    11
    Views:
    705
    Roberto Bonvallet
    Sep 12, 2007
  3. Replies:
    6
    Views:
    135
  4. David Filmer
    Replies:
    19
    Views:
    246
    Kevin Collins
    May 21, 2004
  5. Sean Nakasone
    Replies:
    1
    Views:
    188
Loading...

Share This Page