I ran into an interesting situation recently where one of our standard cleanup scripts of Oracle tracefiles, logs, etc. could not find an entry in /etc/oratab even though it was there. The problem was that "grep" didn't know what color $ORACLE_SID was.
Obviously $ORACLE_SID isn't set to a specific color but when you color options with "grep" those special characters that display colors can come back to bite you. For example, lets say you have are using ASM for an Oracle database and want to switch your environment to point to that ASM instance and do so programmatically. The following shows the instance exists and is also defined in "/etc/oratab":
To switch the environment in a script you can simply tell "oraenv" to not prompt for input after you set $ORACLE_SID:
And when running the simple script:
Wait! I validated that the instance I was going to use, "+ASM", existed in "/etc/oratab". Why couldn't "oraenv" find the value? At first when I tried debugging, by making a copy of "/usr/local/bin/oraenv" and in that local copy I added "set -x", then re-running my script to see what was going on, I didn't see the problem:
Just for yucks I decided to send the output to a logfile and review the results in "vi" and sure enough, that helped identify the problem:
The highlighted escape sequences are adding color to the string. And why would that be done? Because someone had added color options to "grep", using "export GREP_OPTIONS="--color=always" ". In my script I used "grep" to find the Oracle instance (in case the environment I was working on had a different value such as a cluster) and the color escape sequences were stored in the variable with the string.
So is there a happy medium where you can use colors to help identify found strings yet not have those colors interfere in cases like I described above? Yes, for the most part. The best option would be to use "--color" or "--color=auto". In either case ("auto" is the default) color is added only when standard output is connected to a terminal, to partially quote the "man" page for "ls". In other words, a command like "grep "^+ASM" /etc/oratab" would include color whereas "grep "^+ASM" /etc/oratab | cut -d\: -f1" would not because output is piped.