From: Kenny Meyer on
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?
From: Chris Rebert on
On Mon, Jul 12, 2010 at 6:29 PM, Kenny Meyer <knny.myer(a)gmail.com> wrote:
> Hello,
>
> I have to figure out if a string is callable on a Linux system. I'm

"callable" seems vague. Is a command string with invalid arguments but
a valid executable "callable"? If no, then there's no general way to
test "callability" without actually running the command.

> actually doing this:
>
>    def is_valid_command(command):
>        retcode = 100 # initialize
>        if command:
>            retcode = subprocess.call(command, shell=True)
>        if retcode is 0:

That should be `== 0`, not `is 0`. The fact that `is 0` just so
happens to work is an implementation detail.

>            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.

Well, you're not gonna be able to get the command's return code
without actually running it (unless perhaps you're referring to a
return code from the shell itself?).

> What are better ways of doing this?

One idea:

from shlex import split as shell_tokenize
from subprocess import check_output

def is_valid_command(command):
try:
executable = shell_tokenize(command)[0]
except (ValueError, IndexError):# invalid shell syntax
return False
return bool(check_output(['which', executable]))# on the PATH?

Cheers,
Chris
--
http://blog.rebertia.com
From: Tim Roberts on
Kenny Meyer <knny.myer(a)gmail.com> wrote:
>
>I have to figure out if a string is callable on a Linux system. I'm
>actually doing this:
>...
>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?

You can do what the "which" command does: split the PATH environment
variable into individual components, and see if the string maps to a file
in any directory in your PATH with the "execute" bit set. That won't catch
shell macros, however. You might be able to use "type" to do that.
--
Tim Roberts, timr(a)probo.com
Providenza & Boekelheide, Inc.
From: Jean-Michel Pichavant on
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.


From: Chris Rebert on
On Tue, Jul 13, 2010 at 4:33 AM, Jean-Michel Pichavant
<jeanmichel(a)sequans.com> wrote:
> Kenny Meyer wrote:
>> 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

`where` seems to be a zsh built-in:
$ # I'm in UR bash
$ nonexistent
-bash: nonexistent: command not found
$ where bash
-bash: where: command not found

And not everyone has zsh installed, so...
I don't see why one shouldn't use the standard `which` *nix command instead..

Also, in retrospect, my suggestion should probably have checked the
return code rather than the output; more efficient and simple that
way.

Cheers,
Chris
--
http://blog.rebertia.com