Created
May 4, 2011 19:01
-
-
Save senko/955792 to your computer and use it in GitHub Desktop.
A Python implementation of the 'which' shell command.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
# A Python implementation of the handy `which' shell command, showing | |
# the full path to a command that's in your path. | |
# | |
# Written by: Senko Rasic <[email protected]> | |
# | |
# Released to Public Domain. Use it as you like. | |
import sys | |
import os | |
import os.path | |
def which(name): | |
""" | |
Find the full path to a binary named 'name' that's located | |
somewhere in the PATH. Returns the file path, or None if there's | |
no executable binary in the PATH with that name. | |
""" | |
path = os.environ.get('PATH', os.defpath) | |
for d in path.split(':'): | |
fpath = os.path.join(d, name) | |
if os.path.exists(fpath) and os.access(fpath, os.X_OK): | |
return fpath | |
return None | |
if __name__ == '__main__': | |
if len(sys.argv) != 2: | |
sys.stderr.write("Usage: %s <command>\n" % sys.argv[0]) | |
sys.exit(-1) | |
else: | |
fpath = which(sys.argv[1]) | |
if fpath: | |
print fpath | |
sys.exit(0) | |
else: | |
sys.stderr.write("Not found in path\n") | |
sys.exit(-1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment