all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* system-command-exists-p
@ 2009-12-07  7:17 Andreas Roehler
  2009-12-07 16:30 ` system-command-exists-p Lennart Borgman
  0 siblings, 1 reply; 8+ messages in thread
From: Andreas Roehler @ 2009-12-07  7:17 UTC (permalink / raw
  To: help-gnu-emacs


Hi,

needed a check if a command exists on system.
Employed the following:

(defun system-command-exists-p (command)
  "Return t if COMMAND is available on system. "
  (let* ((cmd (format "type %s" command)))
    (eq 0 (shell-command cmd))))

Any comments/suggestions?

Andreas

--
https://code.launchpad.net/s-x-emacs-werkstatt/
http://bazaar.launchpad.net/~a-roehler/python-mode/python-mode.el/





^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: system-command-exists-p
       [not found] <mailman.12411.1260170302.2239.help-gnu-emacs@gnu.org>
@ 2009-12-07 10:13 ` Pascal J. Bourguignon
  2009-12-07 12:02   ` system-command-exists-p Andreas Röhler
       [not found]   ` <mailman.12419.1260187409.2239.help-gnu-emacs@gnu.org>
  2009-12-07 16:00 ` system-command-exists-p Stefan Monnier
  1 sibling, 2 replies; 8+ messages in thread
From: Pascal J. Bourguignon @ 2009-12-07 10:13 UTC (permalink / raw
  To: help-gnu-emacs

Andreas Roehler <andreas.roehler@online.de> writes:

> Hi,
>
> needed a check if a command exists on system.
> Employed the following:
>
> (defun system-command-exists-p (command)
>   "Return t if COMMAND is available on system. "
>   (let* ((cmd (format "type %s" command)))
>     (eq 0 (shell-command cmd))))
>
> Any comments/suggestions?

Use %S or (shell-quote-argument command).

type is a built-in command specific to the sh familly of shells.
Perhaps it would be a good idea to explicitely call up sh?

It may be useful to return the path of the command:

(defun system-command-exists-p (command)
  "Return t if COMMAND is available on system. "
  (let* ((cmd (format "/bin/sh -c 'type %s'" 
                      (shell-quote-argument command))))
    (when (eql 0  (shell-command cmd))
      (let ((result (shell-command-to-string cmd)))
        (if (string-match "^.* is \\(.*\\)\n$"result)
            (match-string 1 result)
            result)))))

(mapcar (function system-command-exists-p)
        '("cat" "type" "foo"))
--> ("/bin/cat" "a shell builtin" nil)



-- 
__Pascal Bourguignon__


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: system-command-exists-p
  2009-12-07 10:13 ` system-command-exists-p Pascal J. Bourguignon
@ 2009-12-07 12:02   ` Andreas Röhler
       [not found]   ` <mailman.12419.1260187409.2239.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 8+ messages in thread
From: Andreas Röhler @ 2009-12-07 12:02 UTC (permalink / raw
  To: Pascal J. Bourguignon; +Cc: help-gnu-emacs

Pascal J. Bourguignon wrote:
> Andreas Roehler <andreas.roehler@online.de> writes:
> 
>> Hi,
>>
>> needed a check if a command exists on system.
>> Employed the following:
>>
>> (defun system-command-exists-p (command)
>>   "Return t if COMMAND is available on system. "
>>   (let* ((cmd (format "type %s" command)))
>>     (eq 0 (shell-command cmd))))
>>
>> Any comments/suggestions?
> 
> Use %S or (shell-quote-argument command).
> 
> type is a built-in command specific to the sh familly of shells.
> Perhaps it would be a good idea to explicitely call up sh?


> 
> It may be useful to return the path of the command:
> 
> (defun system-command-exists-p (command)
>   "Return t if COMMAND is available on system. "
>   (let* ((cmd (format "/bin/sh -c 'type %s'" 
>                       (shell-quote-argument command))))
>     (when (eql 0  (shell-command cmd))
>       (let ((result (shell-command-to-string cmd)))
>         (if (string-match "^.* is \\(.*\\)\n$"result)
>             (match-string 1 result)
>             result)))))
> 
> (mapcar (function system-command-exists-p)
>         '("cat" "type" "foo"))
> --> ("/bin/cat" "a shell builtin" nil)
> 
> 
> 

Thanks a lot.

Concerning the  precise kind-of-"shell"-question, I'm not sure.
Case is to write a check for pack-unpack functions.

Presently dired-do-compress uses a hard-encoded "gzip", replaced here by
a choice so far.

Then testing all compressing/uncompressing possibilities, it turns out some might not be installed.

type uncompress =>
uncompress is /usr/bin/uncompress
type compress =>
bash: type: compress: not found

Test should not fail, if these programms are not installed, rather give a message.
So far the issue here.

Think the shell-in-use should not matter, resp. will be selected already.

What about to write:

(cmd (format (concat (getenv "SHELL") " -c 'type %s'")

Thanks again

Andreas




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: system-command-exists-p
       [not found]   ` <mailman.12419.1260187409.2239.help-gnu-emacs@gnu.org>
@ 2009-12-07 13:44     ` Pascal J. Bourguignon
  2009-12-07 16:22       ` system-command-exists-p Andreas Politz
  0 siblings, 1 reply; 8+ messages in thread
From: Pascal J. Bourguignon @ 2009-12-07 13:44 UTC (permalink / raw
  To: help-gnu-emacs

Andreas Röhler <andreas.roehler@easy-emacs.de> writes:

> Pascal J. Bourguignon wrote:
>> Andreas Roehler <andreas.roehler@online.de> writes:
>> 
>>> Hi,
>>>
>>> needed a check if a command exists on system.
>>> Employed the following:
>>>
>>> (defun system-command-exists-p (command)
>>>   "Return t if COMMAND is available on system. "
>>>   (let* ((cmd (format "type %s" command)))
>>>     (eq 0 (shell-command cmd))))
>>>
>>> Any comments/suggestions?
>> 
>> Use %S or (shell-quote-argument command).
>> 
>> type is a built-in command specific to the sh familly of shells.
>> Perhaps it would be a good idea to explicitely call up sh?
>
>
>> 
>> It may be useful to return the path of the command:
>> 
>> (defun system-command-exists-p (command)
>>   "Return t if COMMAND is available on system. "
>>   (let* ((cmd (format "/bin/sh -c 'type %s'" 
>>                       (shell-quote-argument command))))
>>     (when (eql 0  (shell-command cmd))
>>       (let ((result (shell-command-to-string cmd)))
>>         (if (string-match "^.* is \\(.*\\)\n$"result)
>>             (match-string 1 result)
>>             result)))))
>> 
>> (mapcar (function system-command-exists-p)
>>         '("cat" "type" "foo"))
>> --> ("/bin/cat" "a shell builtin" nil)
>> 
>> 
>> 
>
> Thanks a lot.
>
> Concerning the  precise kind-of-"shell"-question, I'm not sure.
> Case is to write a check for pack-unpack functions.
>
> Presently dired-do-compress uses a hard-encoded "gzip", replaced here by
> a choice so far.
>
> Then testing all compressing/uncompressing possibilities, it turns out some might not be installed.
>
> type uncompress =>
> uncompress is /usr/bin/uncompress
> type compress =>
> bash: type: compress: not found
>
> Test should not fail, if these programms are not installed, rather give a message.
> So far the issue here.
>
> Think the shell-in-use should not matter, resp. will be selected already.
>
> What about to write:
>
> (cmd (format (concat (getenv "SHELL") " -c 'type %s'")

My point was that SHELL might be csh or tcsh which don't have a type
command built-in, or even scsh or clash (clisp) who don't even use the
same syntax!  Or yet something else.

So you can just make the assumption that SHELL is a sh-compatible
shell, and go ahead, or try to run sh yourself.

After all, people who use csh or tcsh just deserve what they get, and
people using exotic shells are expected to know what they're doing ;-)

-- 
__Pascal Bourguignon__


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: system-command-exists-p
       [not found] <mailman.12411.1260170302.2239.help-gnu-emacs@gnu.org>
  2009-12-07 10:13 ` system-command-exists-p Pascal J. Bourguignon
@ 2009-12-07 16:00 ` Stefan Monnier
  2009-12-07 18:57   ` system-command-exists-p Andreas Röhler
  1 sibling, 1 reply; 8+ messages in thread
From: Stefan Monnier @ 2009-12-07 16:00 UTC (permalink / raw
  To: help-gnu-emacs

> needed a check if a command exists on system.

executable-find?


        Stefan



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: system-command-exists-p
  2009-12-07 13:44     ` system-command-exists-p Pascal J. Bourguignon
@ 2009-12-07 16:22       ` Andreas Politz
  0 siblings, 0 replies; 8+ messages in thread
From: Andreas Politz @ 2009-12-07 16:22 UTC (permalink / raw
  To: help-gnu-emacs

pjb@informatimago.com (Pascal J. Bourguignon) writes:

> Andreas Röhler <andreas.roehler@easy-emacs.de> writes:
>
>> Pascal J. Bourguignon wrote:
>>> Andreas Roehler <andreas.roehler@online.de> writes:
>>> 
>>>> Hi,
>>>>
>>>> needed a check if a command exists on system.
>>>> Employed the following:
>>>>
>>>> (defun system-command-exists-p (command)
>>>>   "Return t if COMMAND is available on system. "
>>>>   (let* ((cmd (format "type %s" command)))
>>>>     (eq 0 (shell-command cmd))))
>>>>
>>>> Any comments/suggestions?
>>> 

`executable-find' ?

-ap





^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: system-command-exists-p
  2009-12-07  7:17 system-command-exists-p Andreas Roehler
@ 2009-12-07 16:30 ` Lennart Borgman
  0 siblings, 0 replies; 8+ messages in thread
From: Lennart Borgman @ 2009-12-07 16:30 UTC (permalink / raw
  To: Andreas Roehler; +Cc: help-gnu-emacs

On Mon, Dec 7, 2009 at 8:17 AM, Andreas Roehler
<andreas.roehler@online.de> wrote:
>
> Hi,
>
> needed a check if a command exists on system.
> Employed the following:
>
> (defun system-command-exists-p (command)
>  "Return t if COMMAND is available on system. "
>  (let* ((cmd (format "type %s" command)))
>    (eq 0 (shell-command cmd))))
>
> Any comments/suggestions?


"type" is a system specific command. It may be worth trying to use
something not system specific to avoid trouble (maybe wasting your
time and other peoples time otherwise).




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: system-command-exists-p
  2009-12-07 16:00 ` system-command-exists-p Stefan Monnier
@ 2009-12-07 18:57   ` Andreas Röhler
  0 siblings, 0 replies; 8+ messages in thread
From: Andreas Röhler @ 2009-12-07 18:57 UTC (permalink / raw
  To: Stefan Monnier; +Cc: help-gnu-emacs

Stefan Monnier wrote:
>> needed a check if a command exists on system.
> 
> executable-find?
> 
> 
>         Stefan
> 
> 

Think that's it, thanks all.

Andreas




^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2009-12-07 18:57 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.12411.1260170302.2239.help-gnu-emacs@gnu.org>
2009-12-07 10:13 ` system-command-exists-p Pascal J. Bourguignon
2009-12-07 12:02   ` system-command-exists-p Andreas Röhler
     [not found]   ` <mailman.12419.1260187409.2239.help-gnu-emacs@gnu.org>
2009-12-07 13:44     ` system-command-exists-p Pascal J. Bourguignon
2009-12-07 16:22       ` system-command-exists-p Andreas Politz
2009-12-07 16:00 ` system-command-exists-p Stefan Monnier
2009-12-07 18:57   ` system-command-exists-p Andreas Röhler
2009-12-07  7:17 system-command-exists-p Andreas Roehler
2009-12-07 16:30 ` system-command-exists-p Lennart Borgman

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.