Find path of original command when overriding it with a shell script?
2
votes
3
answers
297
views
I'm writing a wrapper script to wrap around the execution of another command; in this case the Chef
kitchen
command. My script will also be called kitchen
and put earlier in $PATH
so that running kitchen
from bash will run my wrapper script instead.
The question is: how do I call the original version of kitchen
? The obvious way, of course, is just to give the full path — just put /usr/bin/kitchen
in the script, but someone else may have it installed at a different path. And of course that precludes any other wrapper scripts—it'd be nice if the solution were stackable.
Two approaches come to mind, both variants on the same theme:
1. In the script, go through $PATH
. Compare each entry to $(dirname "$0")
using stat
using device number & inode number to see if its the same directory. If so, remove it from $PATH
. Then can just call kitchen
, because it shouldn't re-call the wrapper script.
2. Similar to #1, but do the $PATH
lookup by hand in the script, and use stat
on each result vs. $0
. Keep skipping until we find ourself, then use the next one found. If we never find ourself, then just exec it normally (to handle the case the wrapper isn't in $PATH
but was executed by providing a path to it)
Is there a better way? Or does code to do this already exist somewhere on a typical Debian or Ubuntu Linux system?
Asked by derobert
(112989 rep)
Sep 22, 2021, 06:30 PM
Last activity: Sep 23, 2021, 09:36 AM
Last activity: Sep 23, 2021, 09:36 AM