Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Curious Symlink question

by patrickrock (Beadle)
on May 02, 2005 at 22:36 UTC ( #453430=perlquestion: print w/replies, xml ) Need Help??

patrickrock has asked for the wisdom of the Perl Monks concerning the following question:

I want to echo out the original linking file from a symlink.
so
test->/some/dir/file.pl
and file.pl will echo out 'test' as the calling link.
Is this possible?

Replies are listed 'Best First'.
Re: Curious Symlink question
by Roy Johnson (Monsignor) on May 02, 2005 at 22:56 UTC
    What you've written suggests that you want a file to know what symlinks there are to it. It does not. For the other direction — determining where a symlink points — use readlink.

    Caution: Contents may have been coded under pressure.
      Roy,

      Yes, precisely. I wondered if there was a way for a file to know what symlink was originally.

      I thought maybe it got passed along or something.

      Sigh, I guessed not, but one can hope.
        If you're calling the program using the symlink, you can use __FILE__ (or, as the others have suggested, $0). That gives you the name of the program called, and it doesn't matter whether it's a symlink or a real file. If you're calling the program using the real file name, no. A file might have numerous symlinks to it, and it doesn't have any place to keep track of them.

        Caution: Contents may have been coded under pressure.
        You can, if you want the symlink used to start the Perl script currently being executed. It's in $0.
Re: Curious Symlink question
by eibwen (Friar) on May 02, 2005 at 22:45 UTC

    Your post is a bit difficult to follow, but $0 will return the name of the code as invoked. To illustrate:

    $ echo "#!/usr/bin/perl\nprint $0;" > tmp
    $ ln -s tmp temp
    $ ./temp
    ./temp

    UPDATE: Beware potential shell interpolation of $0. The ./tmp file should contain the follow two lines:

    #!/usr/bin/perl -w print $0

      yes! brilliant. the $0 var worked just as you guys described.

      Sorry my question was confusing. Even apparently to myself.

      Thanks for your help, and if you want here is the longer story.

      Oracle applications 10.7 provides a way to feed arguments from oracle apps to a shell script.

      It works like this

      1. Make your shell script with a .prog extenstion. so test.prog

      2. make a symlink to a special oracle binary like so using the name of your .prog so:
      ln -s fnddescr test

      then define test in oracle.

      you call test from oracle, give it your arguments, and then oracle calls test which calls fnddescr which parses the oracle payload, and hands the arguments back to test.prog.

      we wanted to know if how fnddescr could know what called it.

      presumably they have something similiar to $0 again thanks for your help!
Re: Curious Symlink question
by tlm (Prior) on May 02, 2005 at 23:43 UTC

    You may want to check out FindBin. It exports the following variables (quoting from the docs):

    • $Bin: path to bin directory from where script was invoked
    • $Script: basename of script from which perl was invoked
    • $RealBin: $Bin with all links resolved
    • $RealScript: $Script with all links resolved

    the lowliest monk

Re: Curious Symlink question
by cbrandtbuffalo (Deacon) on May 03, 2005 at 11:46 UTC
    The brute-force approach would be to search your entire file system with something like merlyn's Find out where symlinks point, and find all the symlinks that expand to the original path you're looking for. Then you know who points to the original script. This would be feasible if you just needed to know at a point in time. Of course you probably wouldn't want to run it often.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://453430]
Approved by friedo
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2023-09-26 15:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?