0
Follow
0
View

How do I compare the results of running Linux commands?

dqszstrain 注册会员
2023-02-27 16:08

Maybe there are extra line breaks or Spaces to get the result, so this won't work. Thanks so much for answering!!

chaishian 注册会员
2023-02-27 16:08
< div class = "md_content_show e397 data - v - 3967" = "" >

solved, thank you very much!

duxinjian44 注册会员
2023-02-27 16:08

Part of this answer references CHATGPT, GPT_Pro better solution
How do you compare the results of running Linux commands? To compare, we first need to get the results of executing a Linux command, then store those results in a variable, and finally use an if statement to compare the values of the two variables.

Example:

QProcess process;
process.start("cat /sys/class/gpio/value");
process.waitForFinished();
QString result = process.readAllStandardOutput();
if (result == "1") {
    // do something
} else {
    // do something else
}
In the code above, we first define a process object using the QProcess class, then use the start() function to execute Linux commands, and use the waitForFinished() function to ensure that the program continues. Finally, the readAllStandardOutput() function is used to read the standard output of the Linux command and store the value in the result variable. Finally, we use an if statement to compare whether the value of the result variable is 1. If it is, the corresponding code is executed, otherwise the other code is executed.
If the answer is helpful, please accept it.
csu123 注册会员
2023-02-27 16:08

the reference answer GPT ᴼ ᴾ ᴱ ᴺ ᴬ ᴵ < br / > when comparing, need to pay attention to the consistency of the data type. In this example, result is of type QString, and 1 is of type integer.

You can convert result to an integer number type and then compare. For example:

QProcess process;
process.start("cat /sys/class/gpio/value");
process.waitForFinished();
QString result = process.readAllStandardOutput().trimmed();  // 去掉两端的空白字符
int resultInt = result.toInt();  // 将result转换成整型数字

if (resultInt == 1) {
    // do something
}

Note that when converting result to an integer number, you need to trim the whitespace characters at both ends using the trimmed() function first, since there may be additional characters such as line breaks or Spaces when executing Linux commands.

About the Author

Question Info

Publish Time
2023-02-27 16:08
Update Time
2023-02-27 16:08

Related Question