English 中文(简体)
如何获得“正常”产出?
原标题:Using time in a bash script: how to get the "normal" output?

如果我使用<代码>时,则我获得的产出与产出一样。

$ time sleep 1

real    0m1.003s
user    0m0.000s
sys     0m0.000s

然而,如果我用双管书写,产出就是这样。

0.00user 0.00system 0:01.00elapsed 0%CPU (0avgtext+0avgdata 2176maxresident)k
0inputs+0outputs (0major+179minor)pagefaults 0swaps

I already found this post, but what I want is to use time in a script, while still getting the same output as if I was using it in a shell.

最佳回答

页: 1

$ echo "time -p sleep 1" > x1

$ bash x1
real 1.01
user 0.00
sys 0.00
问题回答
0.00user 0.00system 0:01.00elapsed 0%CPU (0avgtext+0avgdata 2176maxresident)k
0inputs+0outputs (0major+179minor)pagefaults 0swaps

......是全球疫苗联盟外部版本tor

real    0m1.003s
user    0m0.000s
sys     0m0.000s

...is the default output produced by the bash builtin time. This builtin version of time is a bash extension: a POSIX-compliant shell is not required to provide a builtin version of time.

然而, POSIX确实规定了一种外部<代码> 时间 通用,该代码可选择<代码>-p,以产生另一种产出格式:

real 1.01
user 0.00
sys 0.00

...and the bash builtin also accepts the -p option to produce the same output format.


<代码>bash 封面应当完全用字母进行,条件是文字实际上由<条码>b<>>> >:

$ cat time.sh
#!/bin/bash
time sleep 1
$ ./time.sh

real    0m1.001s
user    0m0.000s
sys     0m0.000s
$

因此,您的文字似乎正在援引外部效用,而不是<条码>bash。

其最可能的原因是:

  1. your script says #!/bin/sh rather than #!/bin/bash; and
  2. the /bin/sh on your machine is not actually bash, but a more lightweight POSIX-compliant shell without the bash extensions (as found on some Linux distributions these days).

The solution is to ensure that the script specifically invokes bash by using #!/bin/bash if it relies on bash extensions.

The difference is that output is not going to terminal when piped.

做冰布所需的终端代码在档案库中不可行。

What did you want to achieve? If you intend to have the screenshot have a look at script





相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...