简体   繁体   English

确定是否安装了C ++命令行程序

[英]Determine if command line program is installed C++

I am trying to find out if a command line program is installed, so that it can be used later. 我试图找出是否安装了命令行程序,以便以后使用。

So far what I have tried is: 到目前为止,我尝试过的是:

int whichReturn = system("command -v THE_CL_PROGRAM >/dev/null && { exit 50; }|| { exit 60; }");
if (whichReturn == 12800) { //system 'apparently' returns the return value *256 (50*256 = 12800)

    //...

}

However it seems that it always returns 60 and so fails. 但是,它似乎总是返回60,因此失败。

Is there an easier way to do this? 有没有更简单的方法可以做到这一点? Or can someone point out where my mistake is please? 还是可以指出我的错误在哪里?

Thanks 谢谢

A complete program using which : 一个完整的程序,使用which

isthere.cpp: isthere.cpp:

#include <iostream>
#include <cstdlib>
#include <sstream>

int main(int argc, char* argv[])
{
        std::ostringstream cmd;
        cmd << "which " << argv[1] << " >/dev/null 2>&1";
        bool isInstalled = (system(cmd.str().c_str()) == 0);
        std::cout << argv[1] << " is "<< ((isInstalled)?"":"NOT ") << "installed! << std::endl;
}

Output: 输出:

$ ./isthere ls
ls is installed!
$ ./isthere grep
grep is installed!
$ ./isthere foo
foo is NOT installed!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM