3John has asked for the wisdom of the Perl Monks concerning the following question:
I have a script that I need to relocate to a new path. The script's guts (C libs that shouldn't be changed) use some code that basically requires the original path name to be there in the C argv[0] (Perl's $0). Let's say /desired/path is the path that the code expects, and /my/newpath is the place I'm trying to run it from.
First I tried:
$0 = "/desired/path";
That worked! But only on some flavors of Linux and not others. The camel book says this isn't portable so that's not too surprising. Then I tried many incarnations of getting execvp to fool the script. My first attempt:
#!/usr/bin/perl
use strict;
use warnings;
my $desired_path = "/desired/path";
if ($0 ne $desired_path) {
warn($0);
exec { $0 } $desired_path, @ARGV;
exit;
}
warn("Success!");
That infinitely recurses since $0 always appears to be /my/newpath. This behavior as all other behavior that follows seems to be independent of Linux flavor unlike the direct setting of $0.
Suspecting the multiple levels of exec indirection between the shell and Perl I tried;
exec { $^X } $desired_path, $0, @ARGV;
No dice. Same recursion. I tried it right in the shell also:
> exec -a /desired/path /my/newpath
Also no dice. Any advice on how to get this problem solved on most flavors of Linux?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Faking Script Names
by shemp (Deacon) on Jul 22, 2005 at 22:29 UTC | |
by ikegami (Patriarch) on Jul 23, 2005 at 00:13 UTC | |
by shemp (Deacon) on Jul 26, 2005 at 17:20 UTC | |
|
Re: Faking Script Names
by kwaping (Priest) on Jul 23, 2005 at 00:24 UTC | |
by 3John (Scribe) on Jul 23, 2005 at 00:51 UTC | |
|
Re: Faking Script Names
by kwaping (Priest) on Jul 23, 2005 at 14:37 UTC |