How to find out if an executable is available in Guile?

I could not find a library function to detect if if an executable is available. The following function does the job but I’m not sure if there is something better in Guile that I could use. Any suggestions are appreciated.

(define (executable-availaible?  executable)
  (let* ((port (open-input-pipe (string-append "which " executable)))
       	 (available? (port-eof? port)))
    (close-pipe port)
    (not available?)))

I think what you are looking for is:

(access? "/path/to/executable" X_OK)

ref: File System (Guile Reference Manual)

Thanks, unfortunately I would have to give it the full path which would probably be unknown. I was looking for something similar to Python’s shutil.which().

I guess I should try to emulate the Python version it seems that it just checks PATH for the command. The test X_OK will be useful for that, so thanks for the suggestion.

@trev I ended up using your suggestion, the Python version uses something similar. Thanks.

(define (which executable )
  (if (access? executable X_OK)
      executable
      (access? (or (search-path (parse-path (getenv "PATH")) executable)
		   "")
	       X_OK)))
2 Likes

@mirkoh Maybe you could contribute that function to Guile upstream. :slight_smile:

I would love to. I’m not familiar with the process required for contributing but I will look it up. Thanks.

1 Like