http://qs321.pair.com?node_id=761679


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!