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?
In reply to Faking Script Names by 3John
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |