Ksh: Test UNIX command without $? everytime

Hello all,

working on Solaris 10 in ksh.

Basicly, in my function, i'm trying to test that all my unix cmd's are true (exit status 0) else you flag the rcControlRule to 1 without going into spagetti mode code testing every $? in a if statement.

The mdb is probably a little tricky cause it will give something like this:

# echo noexec_user_stack/D | mdb -k
noexec_user_stack:
noexec_user_stack: 1

And i'm looking for something like this:

# echo noexec_user_stack/D | mdb -k
noexec_user_stack:
noexec_user_stack: 0

Bottom line i just want to know how you guys evaluate unix command without testing $? everytime... there must be a simpler way. A best practice somewhere that i miss.

Bellow code doesn't work but basicly explain what i'm trying to do.

CONFIG_FILE="/etc/system"
STACK=$(grep -q "^set noexec_user_stack=1" ${CONFIG_FILE})
STACK_LOG=$(grep -q "^set noexec_user_stack_log=1" ${CONFIG_FILE})
MDB=$(echo "noexec_user_stack/D" | mdb -k)

check_config () {
        if [[ ${STACK} -a ${STACK_LOG} -a ${MDB} ]]; then
                rcControlRule=0
        else
                rcControlRule=1
        fi
        return ${rcControlRule}
}

Maybe i should use something like:

set -A services "grep -q \"^set noexec_user_stack=1\" ${CONFIG_FILE}" "grep -q \"^set noexec_user_stack_log=1\" ${CONFIG_FILE}" "echo \"noexec_user_stack/D\" | mdb -k"

check_service () {
        i=0
        while [[ $i -lt "${#services[@]}" ]] && [[ rcControlRule -ne 1 ]]; do
                if  ! "${services[$i]}"  > /dev/null 2>&1; then
                        rcControlRule=1
                else
                        rcControlRule=0
                fi
        (( i+=1 ))
        done
        return ${rcControlRule}
}

Thanks.

Eric

There are many ways to Rome. For example you could try something like this:

CONFIG_FILE="/etc/system"
if
  grep -q "^set noexec_user_stack=1" ${CONFIG_FILE}     &&
  grep -q "^set noexec_user_stack_log=1" ${CONFIG_FILE} &&
  echo "noexec_user_stack/D" | mdb -k >/dev/null 2>&1 
then
  echo all commands are OK
fi

or this:

checkconfig() {
  grep -q "^set noexec_user_stack=1" ${CONFIG_FILE}     &&
  grep -q "^set noexec_user_stack_log=1" ${CONFIG_FILE} &&
  echo "noexec_user_stack/D" | mdb -k >/dev/null 2>&1 
}

if checkconfig; then
  echo OK
fi
1 Like

grep -q turns a match into an exit code, and does not return the matching string.
The exit code is best directly tested in the if statement, like in Scrutinizers post.
The exit code of mdb is a bit strange, so maybe its output should be taken by another grep -q :

if
 ...
 echo "noexec_user_stack/D" | mdb -k 2>/dev/null | grep -q .
then
 ...

Or even should require the value 1 :

 echo "noexec_user_stack/D" | mdb -k 2>/dev/null | grep -qw 1

BTW only /usr/xpg4/bin/grep has -q (not /usr/bin/grep).

1 Like

Many thanks to you guys!

Scrutinizer: love the way you've turned the TRUE into a function. Very clean.

MIG: Never used the -w for grep. Works perfectly (i knew about the grep since i work on Solaris all the time)

Thanks again.