sathiya.sw has asked for the wisdom of the Perl Monks concerning the following question:

I would like to verify that an external command is available or not, strictly without executing it.
I know there are several ways for executing an external command, as described here executing_external_commands.

Why i don't want to execute and check ?
Because, i wish to check commands such as rm, tar. If i want to check the rm by executing then, i should create a temporary file and for that i should whether i have permission or not and the process goes recursive.
If it is tar, it may initiate archiving a big directory. So to avoid all those things i would like to check whether a command exists, and the current user can execute it or not in effective manner.

Any good option is appreciated.
-- Sathiyamoorthy
  • Comment on How to find whether an external commands exists

Replies are listed 'Best First'.
Re: How to find whether an external commands exists
by Anonymous Monk on May 04, 2009 at 06:24 UTC
    which, File::Which, IPC::Cmd
    use File::Which; # exports which() my %paths; $paths{rm} = which('rm'); $paths{tar} = which('tar');
    use IPC::Cmd qw[can_run run]; my $full_path = can_run('wget') or warn 'wget is not installed!';
Re: How to find whether an external commands exists
by graff (Chancellor) on May 04, 2009 at 06:24 UTC
    Most unix-style command-line tools have a particular option to allow running the tool without actually doing anything -- e.g. just report the version number of the tool (like 'perl -v').

    For tools that don't follow that general rule, you could use $ENV{PATH} to see if a file with a chosen name happens to exist in any of the user's current execution-path directories -- something like:

    my $tool_name = "tar"; # simple example my $tool_path = ''; for my $path ( split /:/, $ENV{PATH} ) { if ( -f "$path/$tool_name" && -x _ ) { print "$tool_name found in $path\n"; $tool_path = "$path/$tool_name"; last; } } die "No $tool_name command available\n" unless ( $tool_path );
    (updated to include the "tool_path" variable and the "die" statement)

    I don't know, but you might need to  split /[;:]/ if you want that to work in the "standard" windows shell as well as on unix(-like) systems.

    If you promise to only use a unix-like environment (incl, cygwin or uwin on windows), you could also depend on the "which" command:

    my $tool_name = "tar"; my $tool_path = `which $tool_name`; die "No $tool_name command available\n" unless ( $tool_path );

    (Another update: D'oh -- I should have known there was a module for this!)

Re: How to find whether an external commands exists
by december (Pilgrim) on May 04, 2009 at 09:02 UTC

    Hello,

    Using 'which' or the path as suggested elsewhere in this thread are definitely ways to get what you want.

    You could just check if the file exists and is executable:

    if (-e '/bin/tar' && -x _) { doSmt(); }

    tar(1) and rm(1) should be in /bin on pretty much any Unix system, so you could get away without checking the path (at least for the commands you listed).

    If I remember correctly, some incarnations of Unix allow executable files to be effectively unreadable, so it might be best to check for existence and "executability", and not readability.

    Another way is to execute the command with an argument of --version or --help, this way you are sure the command executes. You don't need to actually provide any files or do something, such as tar'ing a directory. At least all common GNU programs should accept the version/help arguments, but try it for the specific commands you want to use.

    Good luck!

Re: How to find whether an external commands exists
by przemo (Scribe) on May 04, 2009 at 06:39 UTC
    Why i don't want to execute and check ? Because, i wish to check commands such as rm, tar. If i want to check the rm by executing then, i should create a temporary file and for that i should whether i have permission or not and the process goes recursive.

    As suggested above, the best thing to check where the binary lives is to use one of the modules mentioned.

    However, there might be situations, where the command does not exist on PATH, like shell aliasing or internal command.

    I would just run the desired command with system and check for the result with $?, as described in system documentation's example.

      there might be situations, where the command does not exist on PATH, like shell aliasing or internal command.
      This is tricky. It is pretty system dependent on what the default shell for system is. If you know it is bash, you could shell out and do a type COMMANDNAME.

      -- 
      Ronald Fischer <ynnor@mm.st>
Re: How to find whether an external commands exists
by toolic (Bishop) on May 04, 2009 at 16:46 UTC