簡體   English   中英

確定是否安裝了C ++命令行程序

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

我試圖找出是否安裝了命令行程序,以便以后使用。

到目前為止,我嘗試過的是:

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)

    //...

}

但是,它似乎總是返回60,因此失敗。

有沒有更簡單的方法可以做到這一點? 還是可以指出我的錯誤在哪里?

謝謝

一個完整的程序,使用which

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;
}

輸出:

$ ./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