K
K
Konstantin2020-02-06 12:24:59
linux
Konstantin, 2020-02-06 12:24:59

bash. How to read the log file into a variable after redirecting "dynamic output" &> file.log?

Hello.
Purpose: in a bash script, execute an external command and write the output and errors to a variable, but without dynamic changes (percentages, speed).
Option via

OUTPUT=`megacmd sync /tmp/site/ mega:/backup/latest/ 2>&1`

megacmd sync /tmp/site/ mega:/backup/latest/ > log.txt 2>&1
OUTPUT=`cat log.txt`

obtained with dynamic strings (see example output below CODE-2) both in a variable and in a file.

The program displays the percentage of the downloaded file and the download speed, it's all in one line.
After the program completes, the output is:
CODE-1
# megacmd sync /tmp/site/ mega:/backup/latest/
Found 1 file(s) to be copied
Copying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 100.00 % of 5.9MB at 154K/s 39s
Successfully sync /tmp/site/ to mega:/backup/latest/ in 39s


where is the line
Copying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 100.00 % of 5.9MB at 154K/s 39s

changed for some time (percentage, speed)

But then a problem appears: if you display the contents in the console, the
cat log.txt
output will be identical to the first example (CODE-1)

And if you open the file in a text editor or read into a variable through,
OUTPUT=$(cat log.txt)
then the OUTPUT variable will contain a completely different result :
CODE-2
Found 1 file(s) to be copied

[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 0.00 % of 5.9MB at 0B/s 0s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 0.00 % of 5.9MB at 0B/s 1s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 0.00 % of 5.9MB at 0B/s 2s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 2.24 % of 5.9MB at 46KB/s 2s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 2.24 % of 5.9MB at 46KB/s 3s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 2.24 % of 5.9MB at 46KB/s 4s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 6.71 % of 5.9MB at 68KB/s 5s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 6.71 % of 5.9MB at 68KB/s 6s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 6.71 % of 5.9MB at 68KB/s 7s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 6.71 % of 5.9MB at 68KB/s 8s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 13.43 % of 5.9MB at 85KB/s 9s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 13.43 % of 5.9MB at 85KB/s 10s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 13.43 % of 5.9MB at 85KB/s 11s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 13.43 % of 5.9MB at 85KB/s 12s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 22.38 % of 5.9MB at 101K/s 12s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 22.38 % of 5.9MB at 101K/s 14s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 22.38 % of 5.9MB at 101K/s 15s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 22.38 % of 5.9MB at 101K/s 16s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 22.38 % of 5.9MB at 101K/s 17s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 22.38 % of 5.9MB at 101K/s 18s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 22.38 % of 5.9MB at 101K/s 19s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 22.38 % of 5.9MB at 101K/s 20s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 22.38 % of 5.9MB at 101K/s 21s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 35.80 % of 5.9MB at 95KB/s 21s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 35.80 % of 5.9MB at 95KB/s 22s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 35.80 % of 5.9MB at 95KB/s 23s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 46.99 % of 5.9MB at 114K/s 24s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 46.99 % of 5.9MB at 114K/s 25s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 46.99 % of 5.9MB at 114K/s 26s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 46.99 % of 5.9MB at 114K/s 27s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 48.53 % of 5.9MB at 104K/s 27s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 64.20 % of 5.9MB at 137K/s 27s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 64.20 % of 5.9MB at 137K/s 28s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 64.20 % of 5.9MB at 137K/s 29s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 64.20 % of 5.9MB at 137K/s 30s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 82.10 % of 5.9MB at 156K/s 30s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 82.10 % of 5.9MB at 156K/s 31s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 82.10 % of 5.9MB at 156K/s 32s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 82.10 % of 5.9MB at 156K/s 33s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 82.10 % of 5.9MB at 156K/s 34s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 82.10 % of 5.9MB at 156K/s 35s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 82.10 % of 5.9MB at 156K/s 36s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 82.10 % of 5.9MB at 156K/s 37s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 100.00 % of 5.9MB at 154K/s 37s 
[2KCopying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 100.00 % of 5.9MB at 154K/s 39s 
Successfully sync /tmp/site/ to mega:/backup/latest/ in 39s


Question: how can I write output/log to a variable, as if I did "cat log.txt" in the linux console (to get the output of COD-1)?
Or is it possible to immediately write the output to a variable as in CODE-1?
Thank you.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
AUser0, 2020-02-06
@AUser0

The output _And_ of the error should be written as follows:

megacmd sync /tmp/site/ mega:/backup/latest/ >log.txt 2>&1

The code "[2K" at the beginning of each "Copying ..." line causes the console to move the text cursor to the beginning of the line. Therefore, each next line is displayed on top of the previous line, and this is why the percentages in this line change, and the screen is not filled with lines.
And here's how to leave only the last one from the entire set of rows, with the maximum percentage... Hmmmm... One of the options:
head -n1 log.txt >log2.txt ; grep 'Copying ' log.txt | tail -n1 >>log2.txt ; tail -n1 log.txt >>log2.txt

S
Saboteur, 2020-02-06
@saboteur_kiev

Have you tried that?

RESULT=$(megacmd sync /tmp/site/ mega:/backup/latest/ 2>&1)
echo "Exit code: $?"
echo "Result: $RESULT"

X
xotkot, 2020-02-06
@xotkot

awk '$1 == "Found" || $1 == "Successfully" || /100.00 %/' log.txt

K
Karpion, 2020-02-07
@Karpion

directly to a variable the output is always empty
You would write how you do it. If you redirect with a "greater than" sign, then it is only to a file. There are two ways to put the output of a command into a variable:
переменная=`команда`
переменная=$(команда)
The team can with arguments. In your case, apparently, it should also contain 2>&1(in fact, it has already been written about).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question