Unsolved how to extract "what follows " from string ??
-
I am posting full response to system command.
○ bluetooth.service - Bluetooth service Loaded: loaded (/lib/systemd/system/bluetooth.service; disabled; vendor preset: enabled) Active: inactive (dead) Docs: man:bluetoothd(8)
I am interested in retrieving and analyzing part of the string "bluetooth.service; disabled;"
I can ask "contains ( bluetooth.service)" but I need to analyze " disabled " because it varies - it can be "enabled" also.
Is my only choice to analyze / check for both using "contains " ?
bluetooth.service; disabled;
bluetooth.service; enabled;Cheers
-
@AnneRanch
is the following code good enough?#include <QCoreApplication> #include <QProcess> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QString serviceName = "bluetooth"; // Replace with the name of the service you want to check // Execute the 'systemctl is-active' command to check the service status QProcess process; process.start("systemctl", QStringList() << "is-active" << serviceName); process.waitForFinished(); QByteArray output = process.readAllStandardOutput(); QString status = output.trimmed(); if (status == "active") { enabled; qDebug() << "Service" << serviceName << "is active."; } else { disabled; qDebug() << "Service" << serviceName << "is not active."; } return 0; }
if you prefer to use contain
after you get the line QString info_str = "Loaded: loaded (/lib/systemd/system/bluetooth.service; disabled; vendor preset: enabled)", do if ( true == info_str.simplified().contains( QString( "bluetooth.service; enabled" ) ) ) { enabled; } else { disabled; } do not forget simplified();
-
@JoeCFD OK, that assumes there are only two options. I can change to "switch" , no problem.
What is the advantage of using "simplified"?
( I can look it up ...) -
@AnneRanch simplilfied() will squeeze possibly more spaces between bluetooth.service; and disabled into only one space. This will make sure contains() call more accurate.