Answer the question
In order to leave comments, you need to log in
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`
# 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
Copying /tmp/site/1.tar -> mega:/backup/latest/1.tar # 100.00 % of 5.9MB at 154K/s 39s
cat log.txt
OUTPUT=$(cat log.txt)
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
Answer the question
In order to leave comments, you need to log in
The output _And_ of the error should be written as follows:
megacmd sync /tmp/site/ mega:/backup/latest/ >log.txt 2>&1
head -n1 log.txt >log2.txt ; grep 'Copying ' log.txt | tail -n1 >>log2.txt ; tail -n1 log.txt >>log2.txt
Have you tried that?
RESULT=$(megacmd sync /tmp/site/ mega:/backup/latest/ 2>&1)
echo "Exit code: $?"
echo "Result: $RESULT"
directly to a variable the output is always emptyYou 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 questionAsk a Question
731 491 924 answers to any question