I'm trying to compare 2 std::string variables in order to determine if my program is running on a Raspbian Linux distro.
The function runs a bash script that returns the results of "cat /etc/os-release | grep ID". After that it splits the results into different words, so I can isolate the word "raspbian" after the word "ID=". Similar to the split function in JavaScript.
Executing the bash script and splitting the results work perfectly. The problem is that my if statement is not picking up the comparison even though the name "raspbian" comes up as expected.
I created the std::string rasp variable just in case the if statement is not working due to the difference in data types, but nothing. I'm definitely doing something wrong.
Hope this makes sense :(
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
bool Utils::CheckIfRaspberry() {
bool isRPi = false;
std::string rasp = "raspbian";
std::string res = Exec("cat /etc/os-release | grep ID");
std::vector<std::string> splitArray;
splitArray = Split(res, "=");
for (unsignedint i = 0; i < splitArray.size(); i += 1) {
if (splitArray[i] == rasp) {
std::cout << "A Raspberry!" << "\n";
isRPi = true;
}
}
return isRPi;
}
Actually, dhayden was on the spot with his answer, it does have to do with a \n.
If I change the rasp variable to let's say "VERSION_ID". It finally works. Why? That token in particular is actually alone, with no new lines.
If you notice the raspbian token on the other hand, it's also including "ID_LIKE". That's the problem. I need to find a better way to isolate particular words in strings. So yeah, that's what's going on.