Redis: who eats my field when the pipeline goes through awk?

Redis: 2.0.4, 2.4.1, ...

I am going to write a Nagios plugin to check the length of a list. Here is my script:

#!/bin/sh

help()
{
    echo "Usage: $0 <host> <port> <key> -w <warning> -c <critical>"
}

case "$1" in
    --help)
        help 
        exit
        ;;
esac

if [ $# -eq 0 ]; then
    help
    exit 3
fi
if [ $# -ne "7" ]; then
    help
    exit 4
fi
if [ $4 !="-w" -o $6 !="-c" ]; then
    help
    exit 5
fi

REDIS_CLI="/usr/local/redis/bin/redis-cli"
LLEN=`$REDIS_CLI -h $1 -p $2 llen $3 | awk '{ print $2 }'`
if [ $LLEN -lt $5 ]; then
    echo "$3.llen:$2 OK - $LLEN | $3.llen:$2=$LLEN;$5;$7"
    exit 0
elif [ $LLEN -ge $5 -a $LLEN -lt $7 ]; then
    echo "$3.llen:$2 WARNING - $LLEN | $3.llen:$2=$LLEN;$5;$7"
    exit 1
elif [ $LLEN -ge "$7" ]; then   
    echo "$3.llen:$2 CRITICAL - $LLEN | $3.llen:$2=$LLEN;$5;$7"
    exit 2
fi

but when /usr/lib64/nagios/plugins/redis_llen.sh 192.168.5.201 2468 -w 90000 -c 100000I started, I got the following error:

/usr/lib64/nagios/plugins/redis_llen.sh: line 31: [: -lt: unary operator expected
/usr/lib64/nagios/plugins/redis_llen.sh: line 34: [: too many arguments
/usr/lib64/nagios/plugins/redis_llen.sh: line 37: [: -ge: unary operator expected

Running it in debug mode, I found that the value LLEN... is empty. Since it llen queue_1returns the correct result:

# /usr/local/redis/bin/redis-cli -h 192.168.5.201 -p 2468 llen queue_1
(integer) 965

Why does the pipeline swallow my fields? (not only awk, but also echo, tee...):

# /usr/local/redis/bin/redis-cli -h 192.168.5.201 -p 2468 llen queue_1 | \
awk '{ print $0 }'
961

I can check the number of fields and print the corresponding one as a workaround:

| awk '{ if (NF == 2) print $2; else print $1 }'`

but I really want to know why this is happening? Is there a null or special character between (interger)and a number?

PS: it seems that another version of Redis (for ex: 1.3.7) does not cause this problem.

+5
1

, , redis-cli , STDOUT. STDOUT TTY, redis-cli . "" :

--raw            Use raw formatting for replies (default when STDOUT is not a tty)

, --raw, "" ( ..). , , , , CSV, redis-cli --csv.

: "" , STDOUT TTY, FAKETTY:

FAKETTY=1 redis-cli llen some_list | awk '{ print $2 }'

redis-cli --raw llen some_list | awk '{ print $1 }'
+8
source

All Articles