NotFromBrooklyn
NotFromBrooklyn

Reputation: 115

Why doesn't grep return TRUE in C++?

I'm adapting some of my code from Bash to C++ and I have no idea why this allways returns FALSE.

#include <iostream>
#include <stdlib.h>

int main() {
    if ( system ("grep -q zswap.enabled=1 /etc/default/grub") ) {
        std::cout << "   zswap.enabled=1 ?: Yes\n";
    }
    else {
        std::cout << "   zswap.enabled=1 ?: No\n"; 
    }
    return 0;
}

Upvotes: 0

Views: 156

Answers (1)

Kilian Foth
Kilian Foth

Reputation: 14346

Because Bash interprets 0 as "success" while C++ interprets it as "false". It's a historical thing that's annoying, but by no means the most annoying thing you'll encounter when porting lengthy shell scripts to C/C++.

Upvotes: 5

Related Questions