Help: How to change PATH variable by array?

A

Amy Lee

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
 
T

Tad McClellan

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?
 
M

Mumia W.

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__
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top