如何检查命令是否可以执行? [英] How to check whether a command can be executed?

查看:121
本文介绍了如何检查命令是否可以执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本会向用户显示一些压缩选项(gzip,zip和bzip)。因为这意味着在Linux和Linux上运行。 Windows和Zlib库将不可用我必须使用Unix命令压缩任何文件。当然,如果脚本在Windows上运行,它可能不会有任何漂亮的命令行压缩工具,因此应用程序不应该向用户显示任何选择。

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.

所以,我想知道的是如何检查命令行工具是否存在。我可以检查可执行文件是否存在,但它是不可靠的(例如,在我的桌面上我使用Gentoo Linux和 zip 命令生活在 / usr / bin ;另一方面,在我的服务器上它存在于 / bin )。

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).

推荐答案

使用 which 命令。例如,使用 which zip 可以找出 zip 的位置,如果存在。它是Linux / Mac的标准配置,也可以 Windows

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.

这篇关于如何检查命令是否可以执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆