0
  • Ubuntu 10.10
  • GNU bash, version 4.1.5(1)-release (i686-pc-linux-gnu)
  • redis-cli shipped with Redis verison 2.0.0

This script does work:

#! /bin/bash

set -e

f=$(echo 'bgrewriteaof_in_progress:0' | grep bgrewriteaof_in_progress | cut -d':' -f2)
test "${f}" = "0" && echo y || echo n
echo $f

Prints:

y
0

This script does not work:

#! /bin/bash

set -e

f=$(redis-cli info | grep bgrewriteaof_in_progress | cut -d':' -f2)
test "${f}" = "0" && echo y || echo n
echo $f

Prints:

n
0

How is this possible? (Note that echo $f prints 0, so the result is there.)

1 Answer 1

1

Found it. The redis-cli info prints information with windows line endings (CRLF). Inserting dos2unix somewhere between pipes does resolve the issue.

1
  • 1
    Some debugging tips: Use the same expression in your actual code and the debug code (you quoted the expansion used with test, but not with echo); this would not affect your particular problem this time, but it can be an issue if your parameter has IFS whitespace (usually SP, HT, or NL). Include an explicit prefix and suffix to emphasize whitespace problems. echo "<<$f>>" would be displayed as >>0 in your case (i.e. the suffix overwrote the prefix because of the trailing CR). You can also examine the raw bytes: printf %s "$f" | xxd (or od, hd, hexdump, etc. in place of xxd). Apr 19, 2011 at 1:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .