简体   繁体   中英

echo does not print a variable in bash

I'm facing a very strange issue. I have 2 files ber_log_before.txt and ber_log_after.txt. Their contents are

ber_log_before.txt

$
BOAR 0 BER:14
BOAR 1 BER:19
BOAR 2 BER:0
BOAR 3 BER:0
BOAR 4 BER:0
BOAR 5 BER:0
BOAR 6 BER:0
BOAR 7 BER:0

$

ber_log_after.txt

$
BOAR 0 BER:24
BOAR 1 BER:29
BOAR 2 BER:0
BOAR 3 BER:0
BOAR 4 BER:0
BOAR 5 BER:0
BOAR 6 BER:0
BOAR 7 BER:0

$

There are $ it's normal.

Then I wrote a basic bash command to parse them

PORT="0 1"
for port in $PORT; do
    VAL1=$(grep "BOAR $port" ber_log_before.txt | cut -f2 -d':')
    VAL2=$(grep "BOAR $port" ber_log_after.txt  | cut -f2 -d':')
    echo 1st val ${VAL1} 2em val ${VAL2}
done

Why 1st val 14 2em val 24 is not echoed???

If I replace

echo 1st val ${VAL1} 2em val ${VAL2}

by

echo 1st val ${VAL1}
echo 2em val ${VAL2}

Then at least I have 2em val echoed.

I guess that awk can do the job, but that's not the question.

This is not a problem actually, or it was me the problem. both files I mentioned are created from a telnet session (with expect command to be more specific). So all lines are ending with \\r and of course echo 1st val 14\\r 2em val 24\\r will 'overwrite' the first part of the echo. So I replace the \\r by \\n and then it's ok.

To understand the problem, I copied my command into a script and use set -x . It was easy to find the \\r.

Sorry for that stupid question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM