Reputation: 168071
When I have an executable ruby script foo
starting with a shebang
foo
#!/usr/bin/env ruby
and call that ruby script from within a bash script bar
as executable (i.e., not calling ruby foo
, but directly foo
), how can I get the full path of bar
from within the ruby script foo
?
If this is not possible, then is it possible if I have a bash script baz
in between so that:
bar
(bash) calls baz
(bash) which calls foo
(ruby)
where bar
calls baz
without any explicit argument and baz
figures out the path of its caller bar
, and passes that as an argument when calling foo
?
For my purpose, it is okay whether or not foo
needs to receive the path information as an argument as long as the original bash script bar
does not need to pass that explicitly.
Upvotes: 0
Views: 176
Reputation: 212178
First, note that the question is not well defined. If bar
is not a unique link to the executable, then there is no unique path. Assuming you don't care about that issue and you just want to know how bar
was accessed and if you are running on Linux that information is available in /proc/pid-of-bar/cmdline
. If foo's parent is the process running bar (it should be, unless you've daemonized or foo
is not a direct descendant), the bar's pid is available to foo in the environment at PPID (although ruby almost certainly provides a better way to access the parent's pid.) So, get your parent's pid and read /proc/parent-pid/cmdline. If bar was invoked as a shell script, the first string of cmdline will be the interpreter (terminated by a null), and the second string will be the path you care about.
Upvotes: 3
Reputation: 1927
If I understand your question $0 is all you need.
Edit: and of course pass this into your ruby script via the bar script.
Upvotes: -1