For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > August 2007 > Help: How to change PATH variable by array?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Help: How to change PATH variable by array?
Amy Lee

2007-08-26, 7:09 pm

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
Tad McClellan

2007-08-26, 7:09 pm

Amy Lee <openlinuxsource@gmail.com> 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/"
Mumia W.

2007-08-26, 10:05 pm

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__
Joe Smith

2007-08-28, 8:24 am

Amy Lee wrote:
> unshift @PATH, $ADD;
> print $_;


Printing $_ that way is definitely not the right thing to do.

$_ = 'PATH='.join(':',@PATH)."\n";
print $OUT $_ or warn "problems writing output file: $!\n";

You've got a lot of logic missing from your program.
1) Open input file for reading (and verify that open() succeeded).
2) Open temp file for writing (and verify that open() succeeded).
3) Read input file a line at a time until EOF.
4) If a real PATH setting line (and not just "PATH=" inside a comment"),
change $_ to the correct value.
5) Write $_ to output file.
6) Close output file, checking for errors.
7) Rename input file from ".bash_profile" to ".bash_profile.old".
8) Rename output file from ".bash_profile.tmp" to ".bash_profile".

For another way of doing it, this is in the perldoc FAQ:

perldoc -q 'change one line'
Found in /usr/lib/perl5/5.8/pods/perlfaq5.pod
How do I change one line in a file/delete a line in a file/insert a
line in the middle of a file/append to the beginning of a file?

-Joe
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com