Timeline for Reliably identifying the path to the directory containing the running script
Current License: CC BY-SA 3.0
5 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| May 12, 2015 at 23:52 | comment | added | Otheus | The ZEN method: pick the file descriptor that most looks like it knows what it's doing. | |
| May 12, 2015 at 23:48 | comment | added | Gilles 'SO- stop being evil' |
lsof -p$$ always “works” if there is a script file, but that doesn't do you any good without a method to pick the right descriptor. “Pick the highest one” works often, but not always and there's no way to detect the edge cases.
|
|
| May 12, 2015 at 23:35 | comment | added | Otheus |
@Gilles Cool. thanks for the caveats. For the edge cases you mentioned, lsof -p $$ might still work, but the fd won't be the last.
|
|
| May 12, 2015 at 23:31 | comment | added | Gilles 'SO- stop being evil' |
That's a nice heuristic, which works for dash, yash, posh, ksh93, mksh, bash and zsh. But it does fail in some edge cases, such as if the script is called with a file descriptor >9 already open. It also doesn't detect that the shell was executed with sh <script-file or sh -c 'script content'.
|
|
| May 12, 2015 at 17:38 | history | answered | Otheus | CC BY-SA 3.0 |