0
lines=""
for (( i=0; i < $lineNum; i++ )); do
  read -t 1 -u $redis_socket line
  lines+=$line
done
echo "$lines"

for example, suppose each line from socket is aaa, bbbb, ccccc I want final output to be like aaabbbbccccc.

but I got aaabc, am I doing something wrong here?

I did try many alternatives in other questions but nothing really works.

sample output lines are like below

redis_version:3.0.7
redis_git_sha1:00000000
redis_git_dirty:0
slave0:ip=127.0.0.1,port=6379,state=online,offset=549974442474,lag=0
....
db1:keys=4190,expires=0,avg_ttl=0

and the final output is

db1:keys=4190,expires=0,avg_ttl=0tl=1200913396e,offset=549974442474,lag=0
Adrian Seungjin Lee
  • 1,646
  • 2
  • 15
  • 28

1 Answers1

1

As seen in the comments, the problem lies in the carriage return of the output you are getting.

Since $line contains a carriage return, given a value, saying lines+=$line you are getting as a result the intersection of $lines and $line, instead of having $line appended to $lines.

line       lines
XXX        XXX
YYYY       XXXY
ZZ         XXXY

With your examples:

line  -> redis_git_dirty:0
lines -> redis_git_dirty:0

line  -> slave0:ip=127.0.0.1,port=6379,state=online,offset=549974442474,lag=0
lines -> redis_git_dirty:0.1,port=6379,state=online,offset=549974442474,lag=0
#        ^^^^^^^^^^^^^^^^^
#           intersection

line  -> db1:keys=4190,expires=0,avg_ttl=0
lines -> db1:keys=4190,expires=0,avg_ttl=0te=online,offset=549974442474,lag=0
#        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#           intersection

etc.

So the solution is to remove this carriage return beforehand:

line=$(echo "line" | tr -d '\r')
lines+="$line"
fedorqui
  • 275,237
  • 103
  • 548
  • 598