unexpected results from `service | grep`

The name of the pictureThe name of the pictureThe name of the pictureClash 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






share|improve this question




















  • 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














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






share|improve this question




















  • 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












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






share|improve this question












$ 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








share|improve this question











share|improve this question




share|improve this question










asked Nov 29 '17 at 4:02









TomRoche

4861617




4861617











  • 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
















  • 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















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










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





share|improve this answer






















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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






























    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





    share|improve this answer


























      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





      share|improve this answer
























        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





        share|improve this answer














        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






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 29 '17 at 12:21

























        answered Nov 29 '17 at 12:16









        bu5hman

        1,164214




        1,164214



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay