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:
- In the script, go through $PATH. Compare each entry to$(dirname "$0")usingstatusing device number & inode number to see if its the same directory. If so, remove it from$PATH. Then can just callkitchen, because it shouldn't re-call the wrapper script.
- Similar to #1, but do the $PATHlookup by hand in the script, and usestaton 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$PATHbut 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?


type -ato find all the places, then you can use the second. For a stackable solution you need to look through the list and choose the one after yourself.