help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: system-command-exists-p


From: Pascal J. Bourguignon
Subject: Re: system-command-exists-p
Date: Mon, 07 Dec 2009 14:44:00 +0100
User-agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/22.3 (darwin)

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__


reply via email to

[Prev in Thread] Current Thread [Next in Thread]