首页 > 解决方案 > Bash:在循环的每次迭代中将时间的标准错误输出到新文件

问题描述

实际上,我只是想在每次循环运行时将时间的标准错误输出到不同的文件,这是我的代码,我不确定为什么当我有 $i. 这是我目前的代码。

(time for i in {1..5}
do
 #some command
done) > temp.txt 2>> $i.txt

下面的代码有效,但每次迭代都没有单独的文件。

(time for i in {1..5}
do
 #some command
done) > temp.txt 2>> output.txt

标签: bash

解决方案


您需要将time命令放在循环内,因为该$i变量不存在于循环外。

for i in {1..5}
do
    time some command 2>>"$i.txt"
done > temp.txt

推荐阅读