Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.
  • 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.)

share

1 Answer 1

up vote 1 down vote accepted

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

share
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). –  Chris Johnsen Apr 19 '11 at 1:53

This site is currently not accepting new answers.

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