Interesting Grep Behavior


This task involved using the grep command to search the /etc/rc.d/init.d/network file for occurances of the test command using the or (-o) boolean operator. The moral of the following story is to keep trying (smart) things, until the solution you desire is reached.

$ cd /etc/rc.d/init.d [Enter]		# move to the directory

$ grep -o network [Enter]		# grep with no escaping
grep: invalid option -- o
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.

$ grep \-o network [Enter]		# grep with one escape char
grep: invalid option -- o
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.

$ grep \-\o network [Enter]		# grep with two escape chars
grep: invalid option -- o
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.

$ man grep [Enter]			# okay, let's try the man page and we find this option

       -e PATTERN, --regexp=PATTERN
              Use  PATTERN as the pattern; useful to protect patterns beginning with -.

$ grep -e -o network [Enter]		# aha, success
[ ! -x /sbin/ipx_internal_net -o ! -x /sbin/ipx_configure ] && IPX=
        if [ "$RUNLEVEL" = "6" -o "$RUNLEVEL" = "0" -o "$RUNLEVEL" = "1" ]; then
                if [ -n "$NFSMTAB" -o -n "$SMBMTAB" -o -n "$NCPMTAB" ] ; then
                [ -n "$DEV_UP$DEV_DOWN$DEV_RECONF$DEV_RECONF_ALIASES" -o \

$ grep \\-o network [Enter]		# another solution suggested by a colleague
[ ! -x /sbin/ipx_internal_net -o ! -x /sbin/ipx_configure ] && IPX=
        if [ "$RUNLEVEL" = "6" -o "$RUNLEVEL" = "0" -o "$RUNLEVEL" = "1" ]; then
                if [ -n "$NFSMTAB" -o -n "$SMBMTAB" -o -n "$NCPMTAB" ] ; then
                [ -n "$DEV_UP$DEV_DOWN$DEV_RECONF$DEV_RECONF_ALIASES" -o \


Notes:



Go Back


©2015, Mark A. Thomas. All Rights Reserved.