问题
I have an script that will show some compress options to the user (gzip, zip, and bzip). Since this is meant to run on both Linux & Windows and the Zlib library won't be available I have to compress any files by using Unix commands. Of course, if the script runs on Windows it probably won't have any of the nice command line compress tools, and thus the app should not show any choices to the user.
So, what I want to know is how to check whether the command line tool exists. I could just check whether the executable file exists, but it's not reliable at all (for instance, on my desktop I use Gentoo Linux and the zip
command lives on /usr/bin
; on the other hand, on my server it lives on /bin
).
回答1:
Use the which
command. For example, use which zip
to find out where zip
lives and if it exists. It comes standard in Linux/Mac, and is available also for Windows.
回答2:
You could just compile zlib for windows and link your program to it... otherwise you'll have to live with hardcoding a few program names into your app, and manually parse the user's PATH variable to find where they are installed.
Or, if you want to use a library, you can use this function from glib: http://www.gtk.org/api/2.6/glib/glib-Miscellaneous-Utility-Functions.html#g-find-program-in-path
But if you're using glib in your app, at that point it's probably easier to just build zlib for windows.
回答3:
Zlib can be installed on all of the above operating systems.
If you look at the manual...
www.php.net/manual/en/zlib.installation.php
It says " Built-in support for zlib on Windows is available with PHP 4.3.0."
Zlib is not enabled by default, you have to enable it manually in php.ini. In order to check if zlib or any other module is enabled see this tutorial:
www.wlscripting.com/tutorial/47
来源:https://stackoverflow.com/questions/3209836/how-to-check-whether-a-command-can-be-executed