bash bash脚本在文件中写入执行时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13176611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:40:37  来源:igfitidea点击:

bash script write executing time in a file

bash

提问by NikM

I need to write the time taken to execute this command in a txt file:

我需要在 txt 文件中写入执行此命令所需的时间:

time ./program.exe

How can I do in bash script?

我该怎么做 bash 脚本?

I try with >> time.txtbut that doesn't work (the output does not go to file and does go to the screen).

我尝试过,>> time.txt但这不起作用(输出不会进入文件并且会进入屏幕)。

回答by Jonathan Leffler

Getting timein bashto write to a file is hard work. It is a bashbuilt-in command. (On Mac OS X, there's an external command, /usr/bin/time, that does a similar job but with a different output format and less recalcitrance.)

入门timebash写入一个文件是艰苦的工作。它是一个bash内置命令。(在 Mac OS X 上,有一个外部命令 ,/usr/bin/time可以完成类似的工作,但具有不同的输出格式和更少的顽固性。)

You need to use:

您需要使用:

(time ./program.exe) 2> time.txt

It writes to standard error (hence the 2>notation). However, if you don't use the sub-shell (the parentheses), it doesn't work; the output still comes to the screen.

它写入标准错误(因此是2>符号)。但是,如果不使用子外壳(括号),则不起作用;输出仍然出现在屏幕上。



Alternatively, and without a sub-shell, you can use:

或者,如果没有子外壳,您可以使用:

{ time ./program.exe; } 2> time.txt

Note the space after the open brace and the semi-colon; both are necessary on a single line. The braces must appear where a command could appear, and must be standalone symbols. (If you struggle hard enough, you'll come up with ...;}|somethingor ...;}2>&1. Both of these identify the brace as a standalone symbol, though. If you try ...;}xyz, the shell will (probably) fail to find a command called }xyz, though.)

注意大括号和分号后面的空格;两者都需要在一行中。大括号必须出现在命令可能出现的地方,并且必须是独立的符号。(如果你足够努力,你会想出...;}|somethingor ...;}2>&1。不过,这两个都将大括号识别为一个独立的符号。不过,如果你尝试...;}xyz,shell 将(可能)找不到名为 的命令}xyz。)



I need to run more command in more terminal. If I do this:

 xterm -xrm '*hold: true' -e (time ./Program.exe) >> time.exe & sleep 2

it doesn't work and tells me Syntax error: "(" unexpected. How do I fix this?

我需要在更多终端中运行更多命令。如果我这样做:

 xterm -xrm '*hold: true' -e (time ./Program.exe) >> time.exe & sleep 2

它不起作用并告诉我Syntax error: "(" unexpected。我该如何解决?

You would need to do something like:

您需要执行以下操作:

xterm -xrm '*hold: true' -e sh -c "(time ./Program.exe) 2> time.txt & sleep 2"

The key change is to run the shell with the script coming from the argument to the -coption; you can replace shwith /bin/bashor an equivalent name. That should get around any 'Syntax error' issues. I'm not quite sure what triggers that error, though, so there may be a simpler and better way to deal with it. It's also conceivable that xterm's -eoption only takes a single string argument, in which case, I suppose you'd use:

关键的变化是使用来自参数到-c选项的脚本运行 shell ;您可以替换sh/bin/bash或等效名称。这应该可以解决任何“语法错误”问题。不过,我不太确定是什么触发了该错误,因此可能有一种更简单更好的方法来处理它。也可以想象xterm's-e选项只接受一个字符串参数,在这种情况下,我想你会使用:

xterm -xrm '*hold: true' -e 'sh -c "(time ./Program.exe) 2> time.txt & sleep 2"'

You can manual bash xtermas well as I can.

你可以xterm和我一样手动 bash 。

I'm not sure why you run the timed program in background mode, but that's your problem, not mine. Similarly, the sleep 2is not obviously necessary if the hold: truekeeps the terminal open.

我不知道你为什么在后台模式下运行定时程序,但那是你的问题,不是我的。同样,sleep 2如果hold: true使终端保持打开状态,则显然没有必要。

回答by hmakholm left over Monica

It's not easy to redirect the output of the bash builtin time.

重定向 bash builtin 的输出并不容易time

One solution is to use the external timeprogram:

一种解决方案是使用外部time程序:

/bin/time --append -o time.txt ./program.exe

(on most systems it's a GNU program, so use info timerather than manto get its documentation).

(在大多数系统上,它是一个 GNU 程序,因此使用info time而不是man获取其文档)。

回答by BIBS

time_elapsed=(time sh -c "./program.exe") 2>&1 | grep "real" | awk '{print $(NF)}'echo time_elapsed > file.txt This command should give you the exact time consumed in bash in a desired file..

time_elapsed= (time sh -c "./program.exe") 2>&1 | grep "real" | awk '{print $(NF)}'echo time_elapsed > file.txt 此命令应该为您提供所需文件中 bash 消耗的确切时间。

You can also redirect this to a file usng 2 > file.txt as explained in another reply.

您还可以将其重定向到文件 usng 2 > file.txt,如另一个回复中所述。

回答by BIBS

Just enclose the command to time in a { .. }:

只需将命令括在时间中{ .. }

{ time ./program.exe; } 2>&1

Of course, the output of builtin time goes to stderr, thus the needed redirection 2>&1.

当然,内置时间的输出进入 stderr,因此需要重定向2>&1

Then, it may appear to be tricky to capture the output, let's use a second { .. }to read the command more easily, this works:

然后,捕获输出似乎很棘手,让我们用一秒钟{ .. }来更轻松地读取命令,这是有效的:

{ { time ./program.exe; } 2>&1; } >> time.txt              # This works.

However, the correct construct should simply have the capture reversed, as this:

但是,正确的构造应该简单地反转捕获,如下所示:

{ time ./program.exe; } >> time.txt  2>&1;                 # Correct.

To close any possible output from the command, redirect it's output to /dev/null, as this:

要关闭命令的任何可能输出,请将其输出重定向到 /dev/null,如下所示:

{ time ./program.exe >/dev/null 2>&1; } >> time.txt 2>&1   # Better.

And, as now there is only output on stderr, we could simply capture just it:

而且,由于现在只有 stderr 输出,我们可以简单地捕获它:

{ time ./program.exe >/dev/null 2>&1; } 2>> time.txt       # Best.

The output from ./program should be redirected, or it may well end inside time.txt.

./program 的输出应该被重定向,否则它可能会在 time.txt 中结束。