Modifying `whatis` to return error code for missing commands Followup to &quot;whatis&quot; returns 0 for missing commands, I am trying to modify /usr/bin/whatis to return a non-zero exit code after an inappropriate command has been entered.  My approach to doing this is to add a return variable in the following segment, and return that variable after the end of the loop

while [ "$1" != "" ]
do
    found=0
    for d in /var/cache/man $manpath /usr/lib
    do
        if [ -f $d/whatis ]
        then
            if grep -"$grepopt1" "$grepopt2""$1" $d/whatis
            then
                found=1
            fi
        fi
    done

    if [ $found = 0 ]
    then
        echo "$1: nothing appropriate"
        **code=1**
    fi

    shift
done | eval ${PAGER:-more -E}
**return $code**


However, since the while loop is piped into eval, it is run in a subshell so code will not be assigned outside the while loop.  I have tried multiple different methods, however, these are all about piping a value INTO the while loop, not piping a value out of the while loop.

A workaround I found is simply removing the entire eval statement, but I would like a method that preserves the original extensibility of whatis
PIPESTATUS is an array. Since you only need $[PIPESTATUS[0]}, the subscript can be omitted.

(
    code=0
    while [ "$1" != "" ]
    do
        found=0
        for d in /var/cache/man $manpath /usr/lib
        do
            if [ -f $d/whatis ]
            then
                if grep -"$grepopt1" "$grepopt2""$1" $d/whatis
                then
                    found=1
                fi
            fi
        done

        if [ $found = 0 ]
        then
            echo "$1: nothing appropriate"
            code=1
        fi

        shift
    done
    exit $code
) | eval ${PAGER:-more -E}
return $PIPESTATUS


From man bash:

   PIPESTATUS
          An array variable (see Arrays below) containing a list  of  exit
          status  values  from the processes in the most-recently-executed
          foreground pipeline (which may contain only a single command).