in reply to Wicked Cool Shell Scripts implemented in Perl

Original: 001-inpath
#!/usr/bin/perl -w use strict; # inpath - verify that a specified program is either valid as-is, # or can be found in the PATH directory list. sub in_path { my ($cmd, $path) = @_; my $found = 0; foreach my $directory (split /:/, $path) { return "exe" if -x "$directory/$cmd"; $found = 1 if -e "$directory/$cmd"; } return $found ? "plain" : "not found"; } sub check_for_command_in_path { my ($command) = @_; if ($command =~ m{^/}) { return 'exe' if -x $command; return -e $command ? 'plain' : 'not found'; } else { in_path($command, $ENV{PATH}); } } die "Usage: $0 command\n" if @ARGV != 1; my $ret = check_for_command_in_path($ARGV[0]); print "Executable $ARGV[0] found in PATH\n" if $ret eq "exe"; print "$ARGV[0] found but not executable" if $ret eq "plain"; print "$ARGV[0] not found in PATH\n" if $ret eq "not found";

Replies are listed 'Best First'.
Re: 001-inpath verify if command is in PATH
by calin (Deacon) on Mar 27, 2006 at 10:51 UTC

    Note: I'm not directing these comments against you (the OP) since you only ported a tool written by somebody else.

    The inpath gizmo does basically the same thing as the shell util which, with a couple additions of very questionable usefulness:

    1. Search in the current directory. Why treat the cwd differently? The cwd is not put in the PATH in most standard shell setups mostly for security reasons. If you want it in your PATH, damn ADD IT! This is not MS-DOS people! Searching for an executable in other locations than the directories in PATH defies the semantics of PATH. The shell will search for an unqualified executable ONLY in directories from PATH, again, do I have to say, unlike MS-DOS and derivatives.
    2. Search for non-executable files in PATH. This is moronic, plain and simple. It serves no other purpose than to complicate the interface. Yeah, anybody can come with any excuse, but the truth is, the cost of feature creep outweights the benefit of usefulness, even for such small a program. Especially when the feature is in fact a non-feature.

    The first addition almost surely betrays the author's roots and nostalgia... The second addition, I'm beginning to think he felt a little "creative", going for a little filler, a little embellishment, particularly when the core "algorithm" was already in place and it cost only a few keystrokes...

    I'm writing this because I'm sick of these "creative" people. Is it just me or does anybody else feel the same? This little crappy program is just a symptom of this "creativity" that IMO contributes to much of the sloppiness, lameness and feature creep we find in programs we use everyday. Watch out for an idiot with a drive.

      yep, you are right in this case, now I wonder how well can I really use that book to learn what people use shell scripts for.
Re: 001-inpath verify if command is in PATH
by davidrw (Prior) on Mar 27, 2006 at 14:09 UTC
    total rewrite using File::Find::Rule:
    use File::Find::Rule; sub in_path { my ($cmd, $path) = @_; my @files = File::Find::Rule->file()->name($cmd)->maxdepth(1)->in( s +plit /:/, $path ) or return 'not found'; return grep(-x $_,@files) ? 'exe' : 'plain'; }
    ditto on calin's comments ... esepecially that this function should just return a boolean, in which case it becomes one line:
    return File::Find::Rule->file()->executable->name($cmd)->maxdepth(1)-> +in( split /:/, $path ) ? 1 : undef;