Kenny Meyer wrote:
Hello,
I have to figure out if a string is callable on a Linux system. I'm
actually doing this:
def is_valid_command(command):
retcode = 100 # initialize
if command:
retcode = subprocess.call(command, shell=True)
if retcode is 0:
print "Valid command."
else:
print "Looks not so good..."
is_valid_command("ls")
Never mind the code, because this is not the original.
The side effect of subprocess.call() is that it *actually* executes
it, but I just need the return code. What are better ways of doing
this?
I'm not sure I get exactly what you're searching for but here's
something that may help.
If you just whant to know if a command (without parameter) is a Linux
command (either a builtin, alias of file exe) you can use the "where"
command and inspect its return code, the command is not executed though.
>where ls
ls is an alias for ls --color=auto -F
ls is /bin/ls
>where apt-get
apt-get is /usr/bin/apt-get
>where doesnotexists
doesnotexists not found
zsh: exit 1
retcode = subprocess.call(command, shell=True)
becomes
retcode = subprocess.call("where " + command)
JM
NB : this does not work with parameters appened to the command.
--
http://mail.python.org/mailman/listinfo/python-list