Version 0 of exec test availability of executables

Updated 2011-05-01 11:09:52 by zhangweiwu

This page addresses the problem how to tell if an executable is available.

Let's say we wish to check availability of 'cdrecord'.

The simplest method is to check with 'which':

 if {[catch {exec which cdrecord} cerr]} {
     puts stderr "'cdrecord' not found in $::env(PATH)"
 } else {
     set ::cdrecord_exec $cerr
     puts stderr "using 'cdrecord' via $cdrecord_exec"
 }

However this method requires a POSIX host system to offer the command "which". In fact even a POSIX system might fail. For example, Cygwin have a hybrid mix of win32 tcl/tk and their POSIX environment. Command "which" would tell cdrecord is available as a symlink, but tcl/tk script running on win32 engine cannot invoke symlink, thus positive answer from 'which' doesn't grantee availability of the executable.

A more complicated method is to actually run the target executable to do a simple thing, like printing its version number. The code is a bit complicated because you need to worry if version number is written on stderr, resulting failed exec. Thus a second check at $::errorCode is necessary:

 if {[catch {exec cdrecord -version}] == 0 || [lindex $::errorCode 0] == "NONE" } {
     puts stderr "'cdrecord' is available"
 }