$ 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
service | grep
當只有 1 行(在每個輸出集中)與模式相符時,為什麼我會得到多於 1 行的輸出行grep
?開頭的幾行有什麼(不好的)魔力嗎[?]
?或者,我錯過了什麼?
FWIW,我正在跑步
$ 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
答案1
在我看來@Sundeep 是對的。
如果您查看腳本是否service
存在導致此行為的重新導向
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 %s\n" "[?]" "$SERVICE:" "unknown" 1>&2
echo " [ ? ] $SERVICE" 1>&2 #<-------------------HERE
continue
else
if [ "$retval" = "0" -a -n "$out" ]; then
#printf " %s %-60s %s\n" "[+]" "$SERVICE:" "running"
echo " [ + ] $SERVICE"
continue
else
#printf " %s %-60s %s\n" "[-]" "$SERVICE:" "NOT running"
echo " [ - ] $SERVICE"
continue
fi
fi
重定向是針對錯誤情況的(服務不支援呼叫status
),因此雖然可見輸出是[ ? ]
“juju”,但實際上是繞過 grep 的重定向。
在命令列上測試這個......
~$ echo "banana" | grep bluetooth #nothing echoed
~$ echo "banana" 1>&2 | grep bluetooth #bingo, get a banana
banana