Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: How to find whether an external commands exists

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


in reply to How to find whether an external commands exists

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!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-03-28 10:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found