I need to check the $? value of grep in the above. If I place a test for $? after the above piped command, it returns success status of grep piped to cut.
Different shells have different mechanisms for dealing with this situation.
Bash 3.0+ and ksh93 have the pipefail option (set -o pipefail) which changes the exit code behavior of pipelines and reports the exit code of the pipeline as the exit code of the last program to return a non-zero exit code.
Bash has the PIPESTATUS array variable which contains a list of exit status values from the processes in the most-recently-executed foreground pipeline
Another makeshift solution would be to invoke a subshell and have it somehow smuggle out the exit status to a dedicated file descriptor. I don't think we want to go there, though.