views:

365

answers:

2

The below check is not working:

if [ $LEN = `expr $CNPROC + $CTotal` ])

and returns expr non-numeric argument shell script.

Always it is going to else. Please tell me what is the mistake.

Earlier I was not using while so the same thing was working fine now suddenly when I did put it in the while loop it is no working.

#!/usr/bin/ksh 
echo "`${BOLD}`    ***** Checking Memory Utilization User*****`${UNBOLD}`"
echo "==================================================="

IFS='|'
cat configMachineDetails.txt | grep -v "^#" | while read MachineType UserName MachineName
do
    export MEMORY_USAGE1=`ssh -f -T ${UserName}@${MachineName} prstat -t -s rss 1 2 | tr '%' ' '| awk '$5>5.0'`
    export LEN=`echo "$MEMORY_USAGE1"|wc -l`
    export CNPROC=`echo "$MEMORY_USAGE1"|grep "NPROC"|wc -l`
    export CTotal=`echo "$MEMORY_USAGE1"|grep "Total"|wc -l`

    if [ $LEN = `expr $CNPROC + $CTotal` ]
    then
        echo "`${BOLD}`**************All usages are normal !!!!!! *************`${UNBOLD}`"
    else
        echo "`${BOLD}`**** Memory(%) is more than 5% in MachineType $MachineType  UserName $UserName
 MachineName  $MachineName   *******`${UNBOLD}`"
        echo "===================================================="
        echo "$MEMORY_USAGE1"
    fi
done
+1  A: 

Put the following statements before your if statement:

echo "[$MEMORY_USAGE1]"
echo "[$LEN]"
echo "[$CNPROC]"
echo "[$CTotal]"

and see what comes out. That error is alnmost invariably caused, surprisingly enough :-), by passing non-numeric data to expr and the echo statements should tell you exactly what the problem is.

For example:

pax> expr 1 + x
expr: non-numeric argument

Based on your comment that your output is:

[ NPROC USERNAME SWAP RSS MEMORY TIME CPU
Total: 143 processes, 2449 lwps, load averages: 1.76, 2.56, 2.94 
NPROC USERNAME SWAP RSS MEMORY TIME CPU
Total: 148 processes, 2443 lwps, load averages: 1.82, 2.57, 2.95]
[ 4]
[ 2]
[ 2]

it's possible that the spaces preceding your numeric values are causing your problems. Try replacing all your occurrences of:

|wc -l

with:

|wc -l|sed 's/ //g'

to get rid of spaces in the wc output.

paxdiablo
With if I should use if [ $LEN = `expr $CNPROC + $CTotal` ] or if [ $LEN -eq `expr $CNPROC + $CTotal` ] The value is [ NPROC USERNAME SWAP RSS MEMORY TIME CPUTotal: 143 processes, 2449 lwps, load averages: 1.76, 2.56, 2.94 NPROC USERNAME SWAP RSS MEMORY TIME CPUTotal: 148 processes, 2443 lwps, load averages: 1.82, 2.57, 2.95][ 4][ 2][ 2]the values are given in asked order.
Kimi
@Kimi: It's the spaces in the values that are causing the problem: `expr " 2" + " 2"` -> "expr: non-numeric argument", `expr "2" + "2"` -> "4"
Dennis Williamson
It shouldn't matter in this case - I tested both and they worked fine on numerics. Technically `-eq` is numeric and `=` is string. But I noticed you have spaces in the variables so you might want to try putting `|sed 's/ //g'` after all the `wc -l` bits (to remove spaces from the `wc` output).
paxdiablo
A: 

Why don't you use ksh's (also works in Bash) builtin arithmetic operations and comparisons?

Instead of:

if [ $LEN = `expr $CNPROC + $CTotal` ]

Try:

if (( LEN == CNPROC + CTotal ))

It's much more forgiving of spaces in the values.

Dennis Williamson