grep exit status in ksh

grep exit status in ksh

am 18.01.2008 17:26:01 von slidge

I'm having a weird problem with grep in a ksh script on a Linux ES 4 box.

The code snippet in question:

STATUS=`$JAVA -User $CxUid -Password $CxPwd \
-address $ArrayId -scope 0 snapview -listclone \
-name $CloneGrp -CloneID $CloneId -IsFractured \
| grep IsFractured`
echo $STATUS | grep 'Yes' >/dev/null 2>&1
if [ $? -ne 0 ]
then
echo "**** Clone $CloneId of $CloneGrp is Not In Split State.****"
exit SYS_EXIT
fi

After the Java command is run, the contents of $STATUS is:

IsFractured: Yes

However, when I run the grep command, $? equals 41136, not 0 as I would
have expected it to be. The same thing happens if I use egrep.

Anybody ever seen this before? I have the same script running on an
identical server and $? returns 0 every time, not 41136.

Re: grep exit status in ksh

am 18.01.2008 18:09:42 von cfajohnson

On 2008-01-18, slidge@slidge.com wrote:
> I'm having a weird problem with grep in a ksh script on a Linux ES 4 box.
>
> The code snippet in question:
>
> STATUS=`$JAVA -User $CxUid -Password $CxPwd \
> -address $ArrayId -scope 0 snapview -listclone \
> -name $CloneGrp -CloneID $CloneId -IsFractured \
> | grep IsFractured`
> echo $STATUS | grep 'Yes' >/dev/null 2>&1
> if [ $? -ne 0 ]
> then
> echo "**** Clone $CloneId of $CloneGrp is Not In Split State.****"
> exit SYS_EXIT
> fi

Don't use grep to tell whether one string is contained in
another. Use a case statement:

case $STATUS in
*Yes*) ;;
*) echo "**** Clone $CloneId of $CloneGrp is Not In Split State.****"
exit $SYS_EXIT
esac

--
Chris F.A. Johnson, author
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence

Re: grep exit status in ksh

am 18.01.2008 18:13:30 von Bill Marcum

On 2008-01-18, slidge@slidge.com wrote:
>
>
> I'm having a weird problem with grep in a ksh script on a Linux ES 4 box.
>
> The code snippet in question:
>
> STATUS=`$JAVA -User $CxUid -Password $CxPwd \
> -address $ArrayId -scope 0 snapview -listclone \
> -name $CloneGrp -CloneID $CloneId -IsFractured \
> | grep IsFractured`
> echo $STATUS | grep 'Yes' >/dev/null 2>&1
> if [ $? -ne 0 ]
> then
> echo "**** Clone $CloneId of $CloneGrp is Not In Split State.****"
> exit SYS_EXIT
> fi
>
> After the Java command is run, the contents of $STATUS is:
>
> IsFractured: Yes
>
> However, when I run the grep command, $? equals 41136, not 0 as I would
> have expected it to be. The same thing happens if I use egrep.
>
Is there an error message if you run the grep command without
">/dev/null 2>&1"?

Re: grep exit status in ksh

am 18.01.2008 20:37:26 von slidge

>> echo $STATUS | grep 'Yes' >/dev/null 2>&1
>> if [ $? -ne 0 ]
>> then
>> echo "**** Clone $CloneId of $CloneGrp is Not In Split State.****"
>> exit SYS_EXIT
>> fi
>
> Don't use grep to tell whether one string is contained in
> another. Use a case statement:
>
> case $STATUS in
> *Yes*) ;;
> *) echo "**** Clone $CloneId of $CloneGrp is Not In Split State.****"
> exit $SYS_EXIT
> esac
>

Cool, I went in and rewrote all those grep statements with case statements
and everything is working fine.

Thanks!