读取终端中的输出并将其与字符串进行比较
Read output in terminal and compare it with a string
如何检查像"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 fortunen");
如果你有一个程序应该在$PATH
的某个地方,但你不知道在哪里,你可以试着运行它:
if (system("fortune --help") != -1)
printf("we have good fortunen");
相关文章:
- 为什么字符串比较的 == 运算符相对于任一字符串长度线性时间(似乎)?
- 字符串比较中的意外输出
- 如何执行不区分大小写的字符串比较?
- 使用 getline(cin, var) 两次在进行字符串比较时会产生错误 (==)
- <<运算符优先级与字符串比较
- ">"运算符和"<"运算符如何进行字符串比较?
- Qt C++11 字符串通过子字符串比较列出交集
- std::out_of_range字符串比较中的异常
- 我正在尝试从输入文件中读取,然后使用它们的子字符串比较字符串的特定部分
- 字符* 与uint8_t的字符串比较
- 用字符串 c++ 比较对向量的元素
- 使用循环进行词典字符串比较
- 如何为C++映射创建自己的字符串比较对象
- 不带比较()的字符串比较
- 与 IF 语句C++的字符串比较?
- 字符串比较在与变量一起使用时给出不同的输出
- 更快的字符串比较排序,用于唯一()
- C 字符串比较“祝您好运”&gt;“再见”
- C 奇怪的行为和字符串比较
- 子字符串与 std::memcmp 或字符串::比较的比较