0

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?


Edit

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.

sawa
  • 165,429
  • 45
  • 277
  • 381
  • You have to pass on $0 as an argument when calling foo. If $0 is not good enough, have a look at: http://stackoverflow.com/a/246128/193892 – Prof. Falken Jun 15 '14 at 14:01
  • @Prof.Falken I want to do that without explicitly passing such argument to `foo`. – sawa Jun 15 '14 at 14:05
  • I don't think that is possible. I mean, what would the mechanism be to transfer that information? – Prof. Falken Jun 15 '14 at 14:12
  • @Prof.Falken I am not sure. Is it possible if I have a bash script `baz`in between so that `bar` (bash) calls `baz` (bash) which calls `foo` (ruby) and let `bar` call` `baz` without `$0` or any other explicit argument but let `baz` figure that out? – sawa Jun 15 '14 at 14:14

2 Answers2

3

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.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • When `bar` has symbolic links, then I would want the realpath. – sawa Jun 15 '14 at 14:27
  • You want to look in `/proc/pid-of-bar/cmdline` – William Pursell Jun 15 '14 at 15:03
  • Okay, I can use `Process.ppid` to get the pid of the parent. – sawa Jun 15 '14 at 15:12
  • Responding to your first comment regarding symbolic links: I'm not talking about symbolic links. If `/p/a/t/h/bar` and `/p/a/t/h/baz` and `/p/a/t/h/qux` are all (hard) links to the same script, then there is no unique "realpath" to the file: there are 3 different paths to the file. – William Pursell Jun 15 '14 at 15:15
  • Thanks for that. I don't have such links, so that would not be an issue. By the way, I followed and looked at the `cmdline` of the parent process. It says: `/bin/bash--noediting-i` Does this mean it does not have the path information? – sawa Jun 15 '14 at 15:19
  • How are you looking at the file? That looks like an artifact of the tool you are using to view. Try `tr \\000 \\n < /proc/.../cmdline` – William Pursell Jun 15 '14 at 15:21
  • In above, I was using a text editor to see it. When I did `tr ...`, it's still the same. The only difference is that `/bin/bash`, `--noediting`, and `-i` appear in different lines. – sawa Jun 15 '14 at 15:24
  • It looks like you are seeing the commandline of an interactive shell. Perhaps bar invoked foo with exec, in which case it is no longer the parent. – William Pursell Jun 15 '14 at 15:28
  • When I call `bash bar`, then I can see something like `bash /path/to/bar` in `cmdline`. When I directly do `bar`, then I see `bash --editing -i`. – sawa Jun 15 '14 at 15:45
-1

If I understand your question $0 is all you need.

Edit: and of course pass this into your ruby script via the bar script.

sestus
  • 1,875
  • 2
  • 16
  • 16