如何检查命令是否存在?
How to check if command is exists?
我需要检测系统中是否存在应用程序。
我用它 std.process 如果可执行命令不存在,下一个代码是异常:
try
{
auto ls = execute(["fooapp"]);
if (ls.status == 0) writeln("fooapp is Exists!\n");
}
catch (Exception e)
{
writeln("exception");
}
有没有更好的方法来检查应用程序是否存在而不抛出异常?
我会非常担心只是 运行ning 一个命令。即使你知道它应该做什么,如果系统上有另一个同名程序(无论是意外还是恶意),你可能会产生奇怪的 - 甚至可能非常糟糕的 - 简单的副作用 运行ning命令。 AFAIK,正确执行此操作将因系统而异,我能建议的最好的办法是利用系统上的任何命令行 shell。
这两个问题的答案似乎提供了有关如何在 Linux 上执行此操作的良好信息,我希望它也适用于 BSD。它甚至可能对 Mac OS X 也有效,但我不知道,因为我不熟悉 Mac OS X 在默认命令行 shells。
How to check if command exists in a shell script?
Check if a program exists from a Bash script
答案似乎可以归结为使用 type
命令,但您应该阅读答案中的详细信息。对于 Windows,快速搜索显示:
Is there an equivalent of 'which' on the Windows command line?
它似乎提供了几种不同的方法来解决 Windows 上的问题。因此,从那里,应该可以找出 Windows 上 运行 的 shell 命令来告诉您特定命令是否存在。
不管OS如何,你需要做的是
bool commandExists(string command)
{
import std.process, std.string;
version(linux)
return executeShell(format("type %s", command)).status == 0;
else version(FreeBSD)
return executeShell(format("type %s", command)).status == 0;
else version(Windows)
static assert(0, "TODO: Add Windows magic here.");
else version(OSX)
static assert(0, "TODO: Add Mac OS X magic here.");
else
static assert(0, "OS not supported");
}
可能在某些系统上,您实际上必须解析命令的输出以查看它是否为您提供了正确的结果,而不是查看状态。不幸的是,这正是系统特定的事情。
您可以在 windows 下使用此功能(所以这是 Windows 魔术 添加,如其他答案中所述......),它检查环境中是否存在文件,默认情况下在 PATH:
string envFind(in char[] filename, string envVar = "PATH")
{
import std.process, std.array, std.path, std.file;
auto env = environment.get(envVar);
if (!env) return null;
foreach(string dir; env.split(";")) {
auto maybe = dir ~ dirSeparator ~ filename;
if (maybe.exists) return maybe.idup;
}
return null;
}
基本用法:
if (envFind("cmd.exe") == "") assert(0, "cmd is missing");
if (envFind("explorer.exe") == "") assert(0, "explorer is missing");
if (envFind("mspaint.exe") == "") assert(0, "mspaintis missing");
我需要检测系统中是否存在应用程序。 我用它 std.process 如果可执行命令不存在,下一个代码是异常:
try
{
auto ls = execute(["fooapp"]);
if (ls.status == 0) writeln("fooapp is Exists!\n");
}
catch (Exception e)
{
writeln("exception");
}
有没有更好的方法来检查应用程序是否存在而不抛出异常?
我会非常担心只是 运行ning 一个命令。即使你知道它应该做什么,如果系统上有另一个同名程序(无论是意外还是恶意),你可能会产生奇怪的 - 甚至可能非常糟糕的 - 简单的副作用 运行ning命令。 AFAIK,正确执行此操作将因系统而异,我能建议的最好的办法是利用系统上的任何命令行 shell。
这两个问题的答案似乎提供了有关如何在 Linux 上执行此操作的良好信息,我希望它也适用于 BSD。它甚至可能对 Mac OS X 也有效,但我不知道,因为我不熟悉 Mac OS X 在默认命令行 shells。
How to check if command exists in a shell script?
Check if a program exists from a Bash script
答案似乎可以归结为使用 type
命令,但您应该阅读答案中的详细信息。对于 Windows,快速搜索显示:
Is there an equivalent of 'which' on the Windows command line?
它似乎提供了几种不同的方法来解决 Windows 上的问题。因此,从那里,应该可以找出 Windows 上 运行 的 shell 命令来告诉您特定命令是否存在。
不管OS如何,你需要做的是
bool commandExists(string command)
{
import std.process, std.string;
version(linux)
return executeShell(format("type %s", command)).status == 0;
else version(FreeBSD)
return executeShell(format("type %s", command)).status == 0;
else version(Windows)
static assert(0, "TODO: Add Windows magic here.");
else version(OSX)
static assert(0, "TODO: Add Mac OS X magic here.");
else
static assert(0, "OS not supported");
}
可能在某些系统上,您实际上必须解析命令的输出以查看它是否为您提供了正确的结果,而不是查看状态。不幸的是,这正是系统特定的事情。
您可以在 windows 下使用此功能(所以这是 Windows 魔术 添加,如其他答案中所述......),它检查环境中是否存在文件,默认情况下在 PATH:
string envFind(in char[] filename, string envVar = "PATH")
{
import std.process, std.array, std.path, std.file;
auto env = environment.get(envVar);
if (!env) return null;
foreach(string dir; env.split(";")) {
auto maybe = dir ~ dirSeparator ~ filename;
if (maybe.exists) return maybe.idup;
}
return null;
}
基本用法:
if (envFind("cmd.exe") == "") assert(0, "cmd is missing");
if (envFind("explorer.exe") == "") assert(0, "explorer is missing");
if (envFind("mspaint.exe") == "") assert(0, "mspaintis missing");