Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: How to find whether an external commands exists

by december (Pilgrim)
on May 04, 2009 at 09:02 UTC ( #761679=note: print w/replies, xml ) Need Help??

in reply to How to find whether an external commands exists


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!

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://761679]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2022-01-17 05:04 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (51 votes). Check out past polls.