Why the "service --status-all | grep mysql" get many unrelated result?
service --status-all
shows its output on both STDOUT (file descriptor 1) and STDERR (file descriptor 2), with the services for which the status can be determined on STDOUT (indicated by [+]
or [-]
) and the services whose status can not be determined on STDERR (indicated by [?]
).
While doing service --status-all | grep mysql
, you are running grep
over only the STDOUT of service --status-all
, that's why everything showing on STDERR are being shown (along with the possible output of grep
).
To get only the desired result, redirect both run redirect both STDOUT and STDERR to grep
:
service --status-all |& grep mysql
Test:
Look at the signs inside []
:
% service --status-all >/dev/null ## Discarding STDOUT
[ ? ] apport
[ ? ] binfmt-support
[ ? ] console-setup
[ ? ] dns-clean
[ ? ] irqbalance
[ ? ] killprocs
[ ? ] kmod
[ ? ] lightdm
[ ? ] mysql
% service --status-all 2>/dev/null ## Discarding STDERR
[ + ] acpid
[ - ] anacron
[ - ] apparmor
[ + ] atd
[ + ] atop
[ + ] avahi-daemon
[ + ] bluetooth
[ - ] brltty
[ + ] cron