elo
elo

Reputation: 541

'cut' output redirect to c++ program

I am trying to change the output of command ping using cut to get some necessary information for me and then redirect it to c++ program with bash pipes.

ping google.com | cut -d " " --fields 8 | ./a.out

C++ program doing such stuff:

int main(){
    string str;
    cin >> str;
    cout << "str:" << str << endl;
}

I suggested, that such command might work, but it doesn't, there is no any output after executing the command above. But it works nicely without using cut. If I use only

ping google.com | ./a.out

my program does output. So I think the problem is my c++ program can't read strings from cut.

How is it possible to fix it?

Upvotes: 0

Views: 189

Answers (1)

P.P
P.P

Reputation: 121407

Yes. ping utility is line-buffered (it does fflush(stdout) after outputting each line).

You can instead set the entire pipeline to use line-buffering by using the stdbuf command from GNU coreutils:

stdbuf -oL -eL bash -c 'ping google.com | cut -d " " --fields 8 | ./a.out'

Upvotes: 1

Related Questions