读取终端中的输出并将其与字符串进行比较

时间:2015-12-15 04:49:42

标签: c++ linux bash ubuntu

如何查看一个类似于“财富”的节目。已经以编程方式安装? 这是我的代码:

if(choice==1)
{
    cout<<"Need to Install program first? [y/n]: ?"<<endl;
    cin>>yn;
    if(yn=='y' || yn=='Y')
    {
            cout<<"Installing..."<<endl;
            cout<<"Enter password if asks:"<<endl;
            system(" sleep 2");
            system("sudo apt-get install fortune");


    }

我试过这个,但无法得到我想要的东西。

dpkg-query -1W fortune  2>&1 |  read line ; do echo $line | say ; done

更新:我已经解决了这个问题,感谢@John Zwinck

1 个答案:

答案 0 :(得分:1)

如果您想知道是否安装了fortune这样的程序,您只需检查它是否存在!

if (access("/usr/bin/fortune", R_OK | X_OK) == 0)
    printf("we have good fortune\n");

如果你的程序应该在$PATH的某个地方,但你不知道在哪里,你可以尝试运行它:

if (system("fortune --help") != -1)
    printf("we have good fortune\n");
相关问题