如何检查像“fortune”这样的程序是否已经以编程方式安装? 这是我的代码:
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
最佳答案
如果您想知道是否安装了像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");
关于c++ - 读取终端中的输出并将其与字符串进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34281331/