Echo BASH Variables rewriting text problem

OS X 10.6.5, BASH

When I ran this:

echo $IP; echo of; echo $IPLINES

I get this output:

219.80.4.150:3128
of
1108

When I ran this:

echo $IP of $IPLINES

I get this output:

 of 1108.150:3128

I expected to receive:

219.80.4.150:3128 of 1108

What can cause the distorted output that I get?

The actual script is this:

#!/bin/bash

IPLINES=`cat a.txt | wc -l | awk '{print $1}'`

if [ $IPLINES > 1 ]; then
    LINE=`expr $RANDOM % $IPLINES + 1`
    IP=`head -$LINE a.txt | tail -1`
    sed -e "${LINE}d" -i .b a.txt

    echo $IP of $IPLINES
fi
+3
source share
2 answers

An amazing premise: you are extracting an IP variable from a TXT file - if the file is Windows or is encoded in Windows style, the lines end with \r\n. You remove the new line, but what if there is one in it \rthat makes you return to the beginning of the line?

Quick dirty fix without question: use echo -n, it suppresses a new line at the end of the echo text.

echo -n $IP; echo -n of; echo -n $IPLINES

, ​​, . $IP.

: OSX, . OSX \r - .

+9

, $IP (\r).

+2

Source: https://habr.com/ru/post/1779645/


All Articles