10

bash/zsh が次のことを行うとどうなるか:

~ » /usr/bin/time -l sleep 1                                                                                                              
    1.00 real         0.00 user         0.00 sys
516096  maximum resident set size
     0  average shared memory size
     0  average unshared data size
     0  average unshared stack size
   145  page reclaims
     0  page faults
     0  swaps
     0  block input operations
     0  block output operations
     0  messages sent
     0  messages received
     0  signals received
     0  voluntary context switches
     2  involuntary context switches
------------------------------------------------------------
~ » time -l sleep 1                                                                                                                       
zsh: command not found: -l
-l sleep 1  0.00s user 0.00s system 52% cpu 0.001 total
------------------------------------------------------------
~ » /usr/bin/time foo                                                                                                                     
foo: No such file or directory
        0.00 real         0.00 user         0.00 sys
------------------------------------------------------------
~ » time foo                                                                                                                              
zsh: command not found: foo
foo  0.00s user 0.00s system 52% cpu 0.001 total

時間の使い方に違いが生じるのはなぜですか?また、zshが実行しようとしているのはなぜ-lですか??

不思議なことに、zsh は言う

~ » which time                                                                                                                            
time: shell reserved word

bash はしませんが:

~ » bash                                                                                                                                  
bash-3.2$ which time
/usr/bin/time
bash-3.2$ time foo
bash: foo: command not found

real    0m0.006s
user    0m0.000s
sys 0m0.003s
bash-3.2$ /usr/bin/time foo
foo: No such file or directory
        0.00 real         0.00 user         0.00 sys
bash-3.2$ time -l sleep 1
bash: -l: command not found

real    0m0.001s
user    0m0.000s
sys 0m0.001s
bash-3.2$ /usr/bin/time -l sleep 1
        1.00 real         0.00 user         0.00 sys
    516096  maximum resident set size
         0  average shared memory size
         0  average unshared data size
         0  average unshared stack size
       144  page reclaims
         0  page faults
         0  swaps
         0  block input operations
         1  block output operations
         0  messages sent
         0  messages received
         0  signals received
         2  voluntary context switches
         2  involuntary context switches
4

3 に答える 3

10

timezsh と bash の両方に組み込まれています。ただし、whichzsh に組み込まれているだけです。bash では、使用すると、シェルのビルトインについて何も考えずwhichに実行されます。/usr/bin/which

したがって、bash では、次を使用する必要があります。

$ type time
time is a shell keyword

time -l ...機能しない理由は、time構文に-lフラグが含まれていないためです。

どちらの場合も、それtimeが組み込み関数であると言うのは正しくありません。これを「予約語」または「シェル キーワード」と呼ぶ方が、パイプライン全体に適用されるため、より正確です。関数または外部コマンドとして実装することはできません。ifその意味では、や などの他の構文要素と似ていwhileます。

于 2013-10-08T00:34:35.327 に答える
4

コマンドが bash 組み込みコマンドかどうかを知る別の方法は、組み込みコマンドを使用することhelpです。副作用は、上記のコマンド (およびサポートされているコマンド ライン引数) に関する情報を取得することです。

bash$ help time
Report time consumed by pipeline's execution.

Execute PIPELINE and print a summary of the real time, user CPU time,
and system CPU time spent executing PIPELINE when it terminates.

Options:
  -p    print the timing summary in the portable Posix format

The value of the TIMEFORMAT variable is used as the output format.

Exit Status:
The return status is the return status of PIPELINE.

非組み込みコマンドの場合、helpそれについて知らないと言います。

bash$ help ls
bash: help: no help topics match `ls'.  Try `help help' or `man -k ls' or `info ls'.
于 2016-07-19T21:33:08.657 に答える
1

time は zsh の組み込み関数です。バッシュにはありません。/usr/bin/time バージョンを使用する場合は、zsh を使用するときにフル パスを指定する必要があります。

zsh で「disable -r」コマンドを使用して、この動作を無効にすることもできます。

于 2013-10-07T23:41:07.407 に答える