通过bash将输出重定向到文件[重复]

Posted

技术标签:

【中文标题】通过bash将输出重定向到文件[重复]【英文标题】:Redirecting output to a file via bash [duplicate] 【发布时间】:2018-11-07 12:18:33 【问题描述】:

我需要在 MIPS 和 ARM 处理器上运行一系列程序执行模拟,为此我使用了一个名为 MPSoCBench 的基准测试。总共会有 200 多个模拟,所以我创建了一个 bash 脚本来逐个执行它并将输出保存到文件中。但是,由于某种原因,它并没有像应有的那样重定向输出。

这是 bash 脚本:

source env.sh
echo "Simulation start"
./MPSoCBench -p=mips -s=stringsearch -i=noc.lt -n=1 -r > output/MIPS/stringsearch/stringsearch-1.out
./MPSoCBench -p=mips -s=stringsearch -i=noc.lt -n=2 -r > output/MIPS/stringsearch/stringsearch-2.out
./MPSoCBench -p=mips -s=stringsearch -i=noc.lt -n=4 -r > output/MIPS/stringsearch/stringsearch-4.out
./MPSoCBench -p=mips -s=stringsearch -i=noc.lt -n=8 -r > output/MIPS/stringsearch/stringsearch-8.out

./MPSoCBench -p=mips -s=dijkstra -i=noc.lt -n=1 -r > output/MIPS/dijkstra/dijkstra-1.out
./MPSoCBench -p=mips -s=dijkstra -i=noc.lt -n=2 -r > output/MIPS/dijkstra/dijkstra-2.out
./MPSoCBench -p=mips -s=dijkstra -i=noc.lt -n=4 -r > output/MIPS/dijkstra/dijkstra-4.out
./MPSoCBench -p=mips -s=dijkstra -i=noc.lt -n=8 -r > output/MIPS/dijkstra/dijkstra-8.out

./MPSoCBench -p=mips -s=sha -i=noc.lt -n=1 -r > output/MIPS/sha/sha-1.out
./MPSoCBench -p=mips -s=sha -i=noc.lt -n=2 -r > output/MIPS/sha/sha-2.out
./MPSoCBench -p=mips -s=sha -i=noc.lt -n=4 -r > output/MIPS/sha/sha-4.out
./MPSoCBench -p=mips -s=sha -i=noc.lt -n=8 -r > output/MIPS/sha/sha-8.out

./MPSoCBench -p=mips -s=basicmath -i=noc.lt -n=1 -r > output/MIPS/basicmath/basicmath-1.out
./MPSoCBench -p=mips -s=basicmath -i=noc.lt -n=2 -r > output/MIPS/basicmath/basicmath-2.out
./MPSoCBench -p=mips -s=basicmath -i=noc.lt -n=4 -r > output/MIPS/basicmath/basicmath-4.out
./MPSoCBench -p=mips -s=basicmath -i=noc.lt -n=8 -r > output/MIPS/basicmath/basicmath-8.out

./MPSoCBench -p=mips -s=susanedges -i=noc.lt -n=1 -r > output/MIPS/susanedges/susanedges-1.out
./MPSoCBench -p=mips -s=susanedges -i=noc.lt -n=2 -r > output/MIPS/susanedges/susanedges-2.out
./MPSoCBench -p=mips -s=susanedges -i=noc.lt -n=4 -r > output/MIPS/susanedges/susanedges-4.out
./MPSoCBench -p=mips -s=susanedges -i=noc.lt -n=8 -r > output/MIPS/susanedges/susanedges-8.out

./MPSoCBench -p=mips -s=susansmoothing -i=noc.lt -n=1 -r > output/MIPS/susansmoothing/susansmoothing-1.out
./MPSoCBench -p=mips -s=susansmoothing -i=noc.lt -n=2 -r > output/MIPS/susansmoothing/susansmoothing-2.out
./MPSoCBench -p=mips -s=susansmoothing -i=noc.lt -n=4 -r > output/MIPS/susansmoothing/susansmoothing-4.out
./MPSoCBench -p=mips -s=susansmoothing -i=noc.lt -n=8 -r > output/MIPS/susansmoothing/susansmoothing-8.out

./MPSoCBench -p=mips -s=susancorners -i=noc.lt -n=1 -r > output/MIPS/susancorners/susancorners-1.out
./MPSoCBench -p=mips -s=susancorners -i=noc.lt -n=2 -r > output/MIPS/susancorners/susancorners-2.out
./MPSoCBench -p=mips -s=susancorners -i=noc.lt -n=4 -r > output/MIPS/susancorners/susancorners-4.out
./MPSoCBench -p=mips -s=susancorners -i=noc.lt -n=8 -r > output/MIPS/susancorners/susancorners-8.out

#Começo das simulações ARM

./MPSoCBench -p=arm -s=stringsearch -i=noc.lt -n=1 -r > output/ARM/stringsearch/stringsearch-1.out
./MPSoCBench -p=arm -s=stringsearch -i=noc.lt -n=2 -r > output/ARM/stringsearch/stringsearch-2.out
./MPSoCBench -p=arm -s=stringsearch -i=noc.lt -n=4 -r > output/ARM/stringsearch/stringsearch-4.out
./MPSoCBench -p=arm -s=stringsearch -i=noc.lt -n=8 -r > output/ARM/stringsearch/stringsearch-8.out

./MPSoCBench -p=arm -s=dijkstra -i=noc.lt -n=1 -r > output/ARM/dijkstra/dijkstra-1.out
./MPSoCBench -p=arm -s=dijkstra -i=noc.lt -n=2 -r > output/ARM/dijkstra/dijkstra-2.out
./MPSoCBench -p=arm -s=dijkstra -i=noc.lt -n=4 -r > output/ARM/dijkstra/dijkstra-4.out
./MPSoCBench -p=arm -s=dijkstra -i=noc.lt -n=8 -r > output/ARM/dijkstra/dijkstra-8.out

./MPSoCBench -p=arm -s=sha -i=noc.lt -n=1 -r > output/ARM/sha/sha-1.out
./MPSoCBench -p=arm -s=sha -i=noc.lt -n=2 -r > output/ARM/sha/sha-2.out
./MPSoCBench -p=arm -s=sha -i=noc.lt -n=4 -r > output/ARM/sha/sha-4.out
./MPSoCBench -p=arm -s=sha -i=noc.lt -n=8 -r > output/ARM/sha/sha-8.out

./MPSoCBench -p=arm -s=basicmath -i=noc.lt -n=1 -r > output/ARM/basicmath/basicmath-1.out
./MPSoCBench -p=arm -s=basicmath -i=noc.lt -n=2 -r > output/ARM/basicmath/basicmath-2.out
./MPSoCBench -p=arm -s=basicmath -i=noc.lt -n=4 -r > output/ARM/basicmath/basicmath-4.out
./MPSoCBench -p=arm -s=basicmath -i=noc.lt -n=8 -r > output/ARM/basicmath/basicmath-8.out

./MPSoCBench -p=arm -s=susanedges -i=noc.lt -n=1 -r > output/ARM/susanedges/susanedges-1.out
./MPSoCBench -p=arm -s=susanedges -i=noc.lt -n=2 -r > output/ARM/susanedges/susanedges-2.out
./MPSoCBench -p=arm -s=susanedges -i=noc.lt -n=4 -r > output/ARM/susanedges/susanedges-4.out
./MPSoCBench -p=arm -s=susanedges -i=noc.lt -n=8 -r > output/ARM/susanedges/susanedges-8.out

./MPSoCBench -p=arm -s=susansmoothing -i=noc.lt -n=1 -r > output/ARM/susansmoothing/susansmoothing-1.out
./MPSoCBench -p=arm -s=susansmoothing -i=noc.lt -n=2 -r > output/ARM/susansmoothing/susansmoothing-2.out
./MPSoCBench -p=arm -s=susansmoothing -i=noc.lt -n=4 -r > output/ARM/susansmoothing/susansmoothing-4.out
./MPSoCBench -p=arm -s=susansmoothing -i=noc.lt -n=8 -r > output/ARM/susansmoothing/susansmoothing-8.out

./MPSoCBench -p=arm -s=susancorners -i=noc.lt -n=1 -r > output/ARM/susancorners/susancorners-1.out
./MPSoCBench -p=arm -s=susancorners -i=noc.lt -n=2 -r > output/ARM/susancorners/susancorners-2.out
./MPSoCBench -p=arm -s=susancorners -i=noc.lt -n=4 -r > output/ARM/susancorners/susancorners-4.out
./MPSoCBench -p=arm -s=susancorners -i=noc.lt -n=8 -r > output/ARM/susancorners/susancorners-8.out
echo "Simulation end"

例如,如果我运行:./MPSoCBench -p=mips -s=susansmoothing -i=noc.lt -n=1 -r

终端输出为:

[['mips'], ['1'], ['susansmoothing'], ['noc.lt']]
./platform.noc.lt.x susansmoothing.mips.x 1

        SystemC 2.3.1-Accellera --- May 26 2018 15:28:48
        Copyright (c) 1996-2014 by all Contributors,
        ALL RIGHTS RESERVED

NOC:

2 Lines

2 Columns

4 Peripherals

1 Masters

3 Slaves

0 Inactive Nodes

4 Wrappers for Masters/Slaves

Printing mesh!

mesh[0][0]: status-> 1  x->0  y->0
mesh[0][1]: status-> 1  x->0  y->1
mesh[1][0]: status-> 1  x->1  y->0
mesh[1][1]: status-> 1  x->1  y->1
Printing Table of routs!
Number of Active Lines in the table of routs: 4 
m[0]: 536870912 0 0
 m[1]: 553648128 0 1
 m[2]: 570425344 1 0
 m[3]: 587202560 1 1
 ArchC: Reading ELF application file: susansmoothing.mips.x


ArchC: -------------------- Starting Simulation --------------------

MPSoCBench: The simulator is prepared.
MPSoCBench: Beggining of time simulation measurement.


--------------------------------------------------------------------
--------------------------  MPSoCBench------------------------------
------------------- Running: susansmoothing ------------------------
--------------- The results will be available in -------------------
--------------------  the output.pgm file---------------------------
--------------------------------------------------------------------


Join Point->0 of 1 processors are not over yet...
Join Point->0 of 1 processors are not over yet...

ArchC: -------------------- Simulation Finished --------------------
Load word counter = 26316758
Store word counter = 12413560
Add Sub counter = 14802018
Mult Div counter = 7571964
Logic Op counter = 638874
Comparision Op counter = 2287692
Branches counter = 3022673
NOP counter = 22570205
SYSTEM Calls counter = 0

Info: /OSCI/SystemC: Simulation stopped by user.
ArchC: Simulation statistics
    Times: 33.87 user, 0.12 system, 33.58 real
    Number of instructions executed: 89623826
    Simulation speed: 2646.11 K instr/s
cache: IC
Cache statistics:
Read:   miss: 7792815 (8.69503%) hit: 81830929 (91.305%)
Write:  miss: 0 (0%) hit: 0 (0%)
Number of block evictions: 2911967
cache: DC
Cache statistics:
Read:   miss: 5480795 (20.9281%) hit: 20707932 (79.0719%)
Write:  miss: 534193 (4.3008%) hit: 11886589 (95.6992%)
Number of block evictions: 973023

Total Time Taken (seconds): 33.581356
Simulation advance (seconds):   0.634481
MPSoCBench: Ending the time simulation measurement.


Lock Access:    138
Memory Reads:   27615606 
Memory Writes:  24841564

TESTING RESULTS

Test Passed.

如果我尝试使用以下命令将此输出重定向到文件:./MPSoCBench -p=mips -s=susansmoothing -i=noc.lt -n=1 -r > output/MIPS/susansmoothing/susansmoothing-1

然后发生了一些奇怪的事情,首先还有终端输出:

        SystemC 2.3.1-Accellera --- May 26 2018 15:28:48
        Copyright (c) 1996-2014 by all Contributors,
        ALL RIGHTS RESERVED
ArchC: Reading ELF application file: susansmoothing.mips.x


ArchC: -------------------- Starting Simulation --------------------

ArchC: -------------------- Simulation Finished --------------------
Load word counter = 26316752
Store word counter = 12413554
Add Sub counter = 14802013
Mult Div counter = 7571962
Logic Op counter = 638873
Comparision Op counter = 2287692
Branches counter = 3022669
NOP counter = 22570200
SYSTEM Calls counter = 0

Info: /OSCI/SystemC: Simulation stopped by user.
ArchC: Simulation statistics
    Times: 35.22 user, 0.12 system, 34.93 real
    Number of instructions executed: 89623796
    Simulation speed: 2544.68 K instr/s
cache: IC
Cache statistics:
Read:   miss: 7793395 (8.69568%) hit: 81830320 (91.3043%)
Write:  miss: 0 (0%) hit: 0 (0%)
Number of block evictions: 2911975
cache: DC
Cache statistics:
Read:   miss: 5481489 (20.9307%) hit: 20707230 (79.0693%)
Write:  miss: 534593 (4.30402%) hit: 11886183 (95.696%)
Number of block evictions: 973878

Total Time Taken (seconds): 34.931891
Simulation advance (seconds):   0.634481
MPSoCBench: Ending the time simulation measurement.


Lock Access:    138
Memory Reads:   27618954 
Memory Writes:  24841552

最后是文件 susansmoothing-1:

./platform.noc.lt.x susansmoothing.mips.x 1

NOC:

2 Lines

2 Columns

4 Peripherals

1 Masters

3 Slaves

0 Inactive Nodes

4 Wrappers for Masters/Slaves

Printing mesh!

mesh[0][0]: status-> 1  x->0  y->0
mesh[0][1]: status-> 1  x->0  y->1
mesh[1][0]: status-> 1  x->1  y->0
mesh[1][1]: status-> 1  x->1  y->1
Printing Table of routs!
Number of Active Lines in the table of routs: 4 
m[0]: 536870912 0 0
 m[1]: 553648128 0 1
 m[2]: 570425344 1 0
 m[3]: 587202560 1 1


--------------------------------------------------------------------
--------------------------  MPSoCBench------------------------------
------------------- Running: susansmoothing ------------------------
--------------- The results will be available in -------------------
--------------------  the output.pgm file---------------------------
--------------------------------------------------------------------


Join Point->0 of 1 processors are not over yet...
Join Point->0 of 1 processors are not over yet...

MPSoCBench: The simulator is prepared.
MPSoCBench: Beggining of time simulation measurement.


TESTING RESULTS

Test Passed.

[['mips'], ['1'], ['susansmoothing'], ['noc.lt']]

就像有些行被重定向而有些则没有,这是怎么回事?也许是多线程?如果是这样,我怎样才能等待程序完成执行,然后将终端输出复制到文件中?

谢谢。

【问题讨论】:

您可能没有在stderr 上进行管道输出。尝试使用&> 而不是>(Bash 4+)。 这不是关于stderr的,我只是试了一下,输出是一样的。也许它与多线程有关。 由于某种原因,Tripp 提出的解决方案不起作用,但使用 &> 确实有效,@hnefatl 您可以将其发布为答案吗? 顺便说一句,您似乎需要了解for p in ARM MIPS; for s in stringsearch dijkstra sha ...etc; do for n in 1 2 3 4; do ./MPSoCBench -p="$p" -s="$s" -i=noc.lt -n="$n" -r > "output/$p/$s/$s-$n.out; done; done; done 【参考方案1】:

程序输出似乎在 STDERR 而不是 STDOUT 上。试试这个:

./MPSoCBench -p=mips -s=dijkstra -i=noc.lt -n=2 -r > output/MIPS/dijkstra/dijkstra-2.out

更改为

./MPSoCBench -p=mips -s=dijkstra -i=noc.lt -n=2 -r > output/MIPS/dijkstra/dijkstra-2.out 2>&1

注意重定向后添加的2>&1。它发送 STDERR -> STDOUT。

【讨论】:

以上是关于通过bash将输出重定向到文件[重复]的主要内容,如果未能解决你的问题,请参考以下文章

在 Bash 中重定向标准错误和标准输出 [重复]

将标准输出重定向到文件[重复]

在 Linux 上使用 Bash 将所有输出重定向到文件? [复制]

从 bash 脚本本身将 stdout 的副本重定向到日志文件

想要通过将输出重定向到变量来检查命令是不是成功

如何将stdout和stderr都重定向到文件[重复]