unexpected results from `service | grep`
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
$ sudo service --status-all | fgrep -e 'bluetooth'
[ ? ] alsa-utils
[ ? ] binfmt-support
[ + ] bluetooth
[ ? ] cpufrequtils
[ ? ] cryptdisks
[ ? ] cryptdisks-early
[ ? ] hdparm
[ ? ] hwclock.sh
[ ? ] kmod
[ ? ] loadcpufreq
[ ? ] networking
[ ? ] plymouth
[ ? ] plymouth-log
[ ? ] pppd-dns
[ ? ] udev-finish
[ ? ] virtualbox-guest-x11
$ sudo service bluetooth stop
[ ok ] Stopping bluetooth: /usr/sbin/bluetoothd.
$ sudo service --status-all | fgrep -e 'bluetooth'
[ ? ] alsa-utils
[ ? ] binfmt-support
[ - ] bluetooth
[ ? ] cpufrequtils
[ ? ] cryptdisks
[ ? ] cryptdisks-early
[ ? ] hdparm
[ ? ] hwclock.sh
[ ? ] kmod
[ ? ] loadcpufreq
[ ? ] networking
[ ? ] plymouth
[ ? ] plymouth-log
[ ? ] pppd-dns
[ ? ] udev-finish
[ ? ] virtualbox-guest-x11
Why am I getting more than 1 output line from service | grep
when only 1 line (in each output set) matches the grep
pattern? Is there something (bad) magic about the lines beginning [?]
? Alternatively, what am I missing?
FWIW, I'm running
$ date
Tue Nov 28 20:51:46 MST 2017
$ uname -rsv
Linux 3.16.0-4-amd64 #1 SMP Debian 3.16.43-2+deb8u5 (2017-09-19)
$ lsb_release -ds
LMDE 2 Betsy
$ cat /etc/debian_version
8.9
$ gcc --version | head -n 1
gcc (Debian 4.9.2-10) 4.9.2
grep services
add a comment |Â
up vote
0
down vote
favorite
$ sudo service --status-all | fgrep -e 'bluetooth'
[ ? ] alsa-utils
[ ? ] binfmt-support
[ + ] bluetooth
[ ? ] cpufrequtils
[ ? ] cryptdisks
[ ? ] cryptdisks-early
[ ? ] hdparm
[ ? ] hwclock.sh
[ ? ] kmod
[ ? ] loadcpufreq
[ ? ] networking
[ ? ] plymouth
[ ? ] plymouth-log
[ ? ] pppd-dns
[ ? ] udev-finish
[ ? ] virtualbox-guest-x11
$ sudo service bluetooth stop
[ ok ] Stopping bluetooth: /usr/sbin/bluetoothd.
$ sudo service --status-all | fgrep -e 'bluetooth'
[ ? ] alsa-utils
[ ? ] binfmt-support
[ - ] bluetooth
[ ? ] cpufrequtils
[ ? ] cryptdisks
[ ? ] cryptdisks-early
[ ? ] hdparm
[ ? ] hwclock.sh
[ ? ] kmod
[ ? ] loadcpufreq
[ ? ] networking
[ ? ] plymouth
[ ? ] plymouth-log
[ ? ] pppd-dns
[ ? ] udev-finish
[ ? ] virtualbox-guest-x11
Why am I getting more than 1 output line from service | grep
when only 1 line (in each output set) matches the grep
pattern? Is there something (bad) magic about the lines beginning [?]
? Alternatively, what am I missing?
FWIW, I'm running
$ date
Tue Nov 28 20:51:46 MST 2017
$ uname -rsv
Linux 3.16.0-4-amd64 #1 SMP Debian 3.16.43-2+deb8u5 (2017-09-19)
$ lsb_release -ds
LMDE 2 Betsy
$ cat /etc/debian_version
8.9
$ gcc --version | head -n 1
gcc (Debian 4.9.2-10) 4.9.2
grep services
I think you are seeing text which is being written to stderr... trysudo service --status-all 2> /dev/null | fgrep -e 'bluetooth'
â Sundeep
Nov 29 '17 at 4:14
@Sundeep not on Debian9, all that output is going to stdout for me there
â thrig
Nov 29 '17 at 4:59
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
$ sudo service --status-all | fgrep -e 'bluetooth'
[ ? ] alsa-utils
[ ? ] binfmt-support
[ + ] bluetooth
[ ? ] cpufrequtils
[ ? ] cryptdisks
[ ? ] cryptdisks-early
[ ? ] hdparm
[ ? ] hwclock.sh
[ ? ] kmod
[ ? ] loadcpufreq
[ ? ] networking
[ ? ] plymouth
[ ? ] plymouth-log
[ ? ] pppd-dns
[ ? ] udev-finish
[ ? ] virtualbox-guest-x11
$ sudo service bluetooth stop
[ ok ] Stopping bluetooth: /usr/sbin/bluetoothd.
$ sudo service --status-all | fgrep -e 'bluetooth'
[ ? ] alsa-utils
[ ? ] binfmt-support
[ - ] bluetooth
[ ? ] cpufrequtils
[ ? ] cryptdisks
[ ? ] cryptdisks-early
[ ? ] hdparm
[ ? ] hwclock.sh
[ ? ] kmod
[ ? ] loadcpufreq
[ ? ] networking
[ ? ] plymouth
[ ? ] plymouth-log
[ ? ] pppd-dns
[ ? ] udev-finish
[ ? ] virtualbox-guest-x11
Why am I getting more than 1 output line from service | grep
when only 1 line (in each output set) matches the grep
pattern? Is there something (bad) magic about the lines beginning [?]
? Alternatively, what am I missing?
FWIW, I'm running
$ date
Tue Nov 28 20:51:46 MST 2017
$ uname -rsv
Linux 3.16.0-4-amd64 #1 SMP Debian 3.16.43-2+deb8u5 (2017-09-19)
$ lsb_release -ds
LMDE 2 Betsy
$ cat /etc/debian_version
8.9
$ gcc --version | head -n 1
gcc (Debian 4.9.2-10) 4.9.2
grep services
$ sudo service --status-all | fgrep -e 'bluetooth'
[ ? ] alsa-utils
[ ? ] binfmt-support
[ + ] bluetooth
[ ? ] cpufrequtils
[ ? ] cryptdisks
[ ? ] cryptdisks-early
[ ? ] hdparm
[ ? ] hwclock.sh
[ ? ] kmod
[ ? ] loadcpufreq
[ ? ] networking
[ ? ] plymouth
[ ? ] plymouth-log
[ ? ] pppd-dns
[ ? ] udev-finish
[ ? ] virtualbox-guest-x11
$ sudo service bluetooth stop
[ ok ] Stopping bluetooth: /usr/sbin/bluetoothd.
$ sudo service --status-all | fgrep -e 'bluetooth'
[ ? ] alsa-utils
[ ? ] binfmt-support
[ - ] bluetooth
[ ? ] cpufrequtils
[ ? ] cryptdisks
[ ? ] cryptdisks-early
[ ? ] hdparm
[ ? ] hwclock.sh
[ ? ] kmod
[ ? ] loadcpufreq
[ ? ] networking
[ ? ] plymouth
[ ? ] plymouth-log
[ ? ] pppd-dns
[ ? ] udev-finish
[ ? ] virtualbox-guest-x11
Why am I getting more than 1 output line from service | grep
when only 1 line (in each output set) matches the grep
pattern? Is there something (bad) magic about the lines beginning [?]
? Alternatively, what am I missing?
FWIW, I'm running
$ date
Tue Nov 28 20:51:46 MST 2017
$ uname -rsv
Linux 3.16.0-4-amd64 #1 SMP Debian 3.16.43-2+deb8u5 (2017-09-19)
$ lsb_release -ds
LMDE 2 Betsy
$ cat /etc/debian_version
8.9
$ gcc --version | head -n 1
gcc (Debian 4.9.2-10) 4.9.2
grep services
asked Nov 29 '17 at 4:02
TomRoche
4861617
4861617
I think you are seeing text which is being written to stderr... trysudo service --status-all 2> /dev/null | fgrep -e 'bluetooth'
â Sundeep
Nov 29 '17 at 4:14
@Sundeep not on Debian9, all that output is going to stdout for me there
â thrig
Nov 29 '17 at 4:59
add a comment |Â
I think you are seeing text which is being written to stderr... trysudo service --status-all 2> /dev/null | fgrep -e 'bluetooth'
â Sundeep
Nov 29 '17 at 4:14
@Sundeep not on Debian9, all that output is going to stdout for me there
â thrig
Nov 29 '17 at 4:59
I think you are seeing text which is being written to stderr... try
sudo service --status-all 2> /dev/null | fgrep -e 'bluetooth'
â Sundeep
Nov 29 '17 at 4:14
I think you are seeing text which is being written to stderr... try
sudo service --status-all 2> /dev/null | fgrep -e 'bluetooth'
â Sundeep
Nov 29 '17 at 4:14
@Sundeep not on Debian9, all that output is going to stdout for me there
â thrig
Nov 29 '17 at 4:59
@Sundeep not on Debian9, all that output is going to stdout for me there
â thrig
Nov 29 '17 at 4:59
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
It appears to me that @Sundeep is correct.
If you look at the script for service
there is a redirect that causes this behaviour
if ! is_ignored_file "$SERVICE"
&& [ -x "$SERVICEDIR/$SERVICE" ]; then
out=$(env -i LANG="$LANG" LANGUAGE="$LANGUAGE" LC_CTYPE="$LC_CTYPE" LC_NUMERIC="$LC_NUMERIC" LC_TIME="$LC_TIME" LC_COLLATE="$LC_COLLATE" LC_MONETARY="$LC_MONETARY" LC_MESSAGES="$LC_MESSAGES" LC_PAPER="$LC_PAPER" LC_NAME="$LC_NAME" LC_ADDRESS="$LC_ADDRESS" LC_TELEPHONE="$LC_TELEPHONE" LC_MEASUREMENT="$LC_MEASUREMENT" LC_IDENTIFICATION="$LC_IDENTIFICATION" LC_ALL="$LC_ALL" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" status 2>&1)
retval=$?
if echo "$out" | egrep -iq "usage:"; then
#printf " %s %-60s %sn" "[?]" "$SERVICE:" "unknown" 1>&2
echo " [ ? ] $SERVICE" 1>&2 #<-------------------HERE
continue
else
if [ "$retval" = "0" -a -n "$out" ]; then
#printf " %s %-60s %sn" "[+]" "$SERVICE:" "running"
echo " [ + ] $SERVICE"
continue
else
#printf " %s %-60s %sn" "[-]" "$SERVICE:" "NOT running"
echo " [ - ] $SERVICE"
continue
fi
fi
The redirect is for the error case (a service does not support a call to status
) and so while the visible output is [ ? ]
the 'juju' is actually the redirect which bypasses the grep.
Testing this on the command line....
~$ echo "banana" | grep bluetooth #nothing echoed
~$ echo "banana" 1>&2 | grep bluetooth #bingo, get a banana
banana
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
It appears to me that @Sundeep is correct.
If you look at the script for service
there is a redirect that causes this behaviour
if ! is_ignored_file "$SERVICE"
&& [ -x "$SERVICEDIR/$SERVICE" ]; then
out=$(env -i LANG="$LANG" LANGUAGE="$LANGUAGE" LC_CTYPE="$LC_CTYPE" LC_NUMERIC="$LC_NUMERIC" LC_TIME="$LC_TIME" LC_COLLATE="$LC_COLLATE" LC_MONETARY="$LC_MONETARY" LC_MESSAGES="$LC_MESSAGES" LC_PAPER="$LC_PAPER" LC_NAME="$LC_NAME" LC_ADDRESS="$LC_ADDRESS" LC_TELEPHONE="$LC_TELEPHONE" LC_MEASUREMENT="$LC_MEASUREMENT" LC_IDENTIFICATION="$LC_IDENTIFICATION" LC_ALL="$LC_ALL" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" status 2>&1)
retval=$?
if echo "$out" | egrep -iq "usage:"; then
#printf " %s %-60s %sn" "[?]" "$SERVICE:" "unknown" 1>&2
echo " [ ? ] $SERVICE" 1>&2 #<-------------------HERE
continue
else
if [ "$retval" = "0" -a -n "$out" ]; then
#printf " %s %-60s %sn" "[+]" "$SERVICE:" "running"
echo " [ + ] $SERVICE"
continue
else
#printf " %s %-60s %sn" "[-]" "$SERVICE:" "NOT running"
echo " [ - ] $SERVICE"
continue
fi
fi
The redirect is for the error case (a service does not support a call to status
) and so while the visible output is [ ? ]
the 'juju' is actually the redirect which bypasses the grep.
Testing this on the command line....
~$ echo "banana" | grep bluetooth #nothing echoed
~$ echo "banana" 1>&2 | grep bluetooth #bingo, get a banana
banana
add a comment |Â
up vote
2
down vote
accepted
It appears to me that @Sundeep is correct.
If you look at the script for service
there is a redirect that causes this behaviour
if ! is_ignored_file "$SERVICE"
&& [ -x "$SERVICEDIR/$SERVICE" ]; then
out=$(env -i LANG="$LANG" LANGUAGE="$LANGUAGE" LC_CTYPE="$LC_CTYPE" LC_NUMERIC="$LC_NUMERIC" LC_TIME="$LC_TIME" LC_COLLATE="$LC_COLLATE" LC_MONETARY="$LC_MONETARY" LC_MESSAGES="$LC_MESSAGES" LC_PAPER="$LC_PAPER" LC_NAME="$LC_NAME" LC_ADDRESS="$LC_ADDRESS" LC_TELEPHONE="$LC_TELEPHONE" LC_MEASUREMENT="$LC_MEASUREMENT" LC_IDENTIFICATION="$LC_IDENTIFICATION" LC_ALL="$LC_ALL" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" status 2>&1)
retval=$?
if echo "$out" | egrep -iq "usage:"; then
#printf " %s %-60s %sn" "[?]" "$SERVICE:" "unknown" 1>&2
echo " [ ? ] $SERVICE" 1>&2 #<-------------------HERE
continue
else
if [ "$retval" = "0" -a -n "$out" ]; then
#printf " %s %-60s %sn" "[+]" "$SERVICE:" "running"
echo " [ + ] $SERVICE"
continue
else
#printf " %s %-60s %sn" "[-]" "$SERVICE:" "NOT running"
echo " [ - ] $SERVICE"
continue
fi
fi
The redirect is for the error case (a service does not support a call to status
) and so while the visible output is [ ? ]
the 'juju' is actually the redirect which bypasses the grep.
Testing this on the command line....
~$ echo "banana" | grep bluetooth #nothing echoed
~$ echo "banana" 1>&2 | grep bluetooth #bingo, get a banana
banana
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
It appears to me that @Sundeep is correct.
If you look at the script for service
there is a redirect that causes this behaviour
if ! is_ignored_file "$SERVICE"
&& [ -x "$SERVICEDIR/$SERVICE" ]; then
out=$(env -i LANG="$LANG" LANGUAGE="$LANGUAGE" LC_CTYPE="$LC_CTYPE" LC_NUMERIC="$LC_NUMERIC" LC_TIME="$LC_TIME" LC_COLLATE="$LC_COLLATE" LC_MONETARY="$LC_MONETARY" LC_MESSAGES="$LC_MESSAGES" LC_PAPER="$LC_PAPER" LC_NAME="$LC_NAME" LC_ADDRESS="$LC_ADDRESS" LC_TELEPHONE="$LC_TELEPHONE" LC_MEASUREMENT="$LC_MEASUREMENT" LC_IDENTIFICATION="$LC_IDENTIFICATION" LC_ALL="$LC_ALL" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" status 2>&1)
retval=$?
if echo "$out" | egrep -iq "usage:"; then
#printf " %s %-60s %sn" "[?]" "$SERVICE:" "unknown" 1>&2
echo " [ ? ] $SERVICE" 1>&2 #<-------------------HERE
continue
else
if [ "$retval" = "0" -a -n "$out" ]; then
#printf " %s %-60s %sn" "[+]" "$SERVICE:" "running"
echo " [ + ] $SERVICE"
continue
else
#printf " %s %-60s %sn" "[-]" "$SERVICE:" "NOT running"
echo " [ - ] $SERVICE"
continue
fi
fi
The redirect is for the error case (a service does not support a call to status
) and so while the visible output is [ ? ]
the 'juju' is actually the redirect which bypasses the grep.
Testing this on the command line....
~$ echo "banana" | grep bluetooth #nothing echoed
~$ echo "banana" 1>&2 | grep bluetooth #bingo, get a banana
banana
It appears to me that @Sundeep is correct.
If you look at the script for service
there is a redirect that causes this behaviour
if ! is_ignored_file "$SERVICE"
&& [ -x "$SERVICEDIR/$SERVICE" ]; then
out=$(env -i LANG="$LANG" LANGUAGE="$LANGUAGE" LC_CTYPE="$LC_CTYPE" LC_NUMERIC="$LC_NUMERIC" LC_TIME="$LC_TIME" LC_COLLATE="$LC_COLLATE" LC_MONETARY="$LC_MONETARY" LC_MESSAGES="$LC_MESSAGES" LC_PAPER="$LC_PAPER" LC_NAME="$LC_NAME" LC_ADDRESS="$LC_ADDRESS" LC_TELEPHONE="$LC_TELEPHONE" LC_MEASUREMENT="$LC_MEASUREMENT" LC_IDENTIFICATION="$LC_IDENTIFICATION" LC_ALL="$LC_ALL" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" status 2>&1)
retval=$?
if echo "$out" | egrep -iq "usage:"; then
#printf " %s %-60s %sn" "[?]" "$SERVICE:" "unknown" 1>&2
echo " [ ? ] $SERVICE" 1>&2 #<-------------------HERE
continue
else
if [ "$retval" = "0" -a -n "$out" ]; then
#printf " %s %-60s %sn" "[+]" "$SERVICE:" "running"
echo " [ + ] $SERVICE"
continue
else
#printf " %s %-60s %sn" "[-]" "$SERVICE:" "NOT running"
echo " [ - ] $SERVICE"
continue
fi
fi
The redirect is for the error case (a service does not support a call to status
) and so while the visible output is [ ? ]
the 'juju' is actually the redirect which bypasses the grep.
Testing this on the command line....
~$ echo "banana" | grep bluetooth #nothing echoed
~$ echo "banana" 1>&2 | grep bluetooth #bingo, get a banana
banana
edited Nov 29 '17 at 12:21
answered Nov 29 '17 at 12:16
bu5hman
1,164214
1,164214
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f407638%2funexpected-results-from-service-grep%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
I think you are seeing text which is being written to stderr... try
sudo service --status-all 2> /dev/null | fgrep -e 'bluetooth'
â Sundeep
Nov 29 '17 at 4:14
@Sundeep not on Debian9, all that output is going to stdout for me there
â thrig
Nov 29 '17 at 4:59