Wednesday, May 15, 2024
 Popular · Latest · Hot · Upcoming
42
rated 0 times [  42] [ 0]  / answers: 1 / hits: 6596  / 1 Year ago, sat, may 6, 2023, 12:19:15

When I try to use grep in this fashion:



service --status-all | grep network


It still lists all the services even though I piped it into grep to restrict it to the "network" string only







After the Fact:



Noticed this article that explains why the command behaves this way, that's what I like about linux, there is always a good reason, but not necessary intuitive



More From » services

 Answers
3

Try:



service --status-all |& grep network


Command-line applications can print to two text streams known as standard out (stdout) and standard error (stderr). By default, the terminal just displays the output for both identically.



Stdout is given the number 1, and stderr is given the number 2. By default, redirection operators, like >, | and < apply only to stdout, number 1.



|& pipes both stdout and stderr together to the process on the right's standard input (stdin) stream, which makes grep work as you expected.



I don't know why service --status-all is printing to stderr here, but in general, having a separate stderr stream is very useful, as it lets you see errors even when stdout is silenced.


[#32274] Sunday, May 7, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
fotres

Total Points: 35
Total Questions: 124
Total Answers: 112

Location: Federated States of Micronesia
Member since Sat, Jun 4, 2022
2 Years ago
fotres questions
Sat, Feb 26, 22, 09:01, 2 Years ago
Sat, Aug 21, 21, 18:51, 3 Years ago
Sun, Oct 3, 21, 22:08, 3 Years ago
;