开发者

expr non-numeric argument shell script

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


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.


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.


Replace

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

with

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

(no space between operand) see if that helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜