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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.