Bashリファレンスマニュアル
time
の使用 予約語として、シェルビルトインのタイミングを許可します 、シェル関数 、およびパイプライン 。外部の時間
コマンドはこれらの時間を簡単に計ることができません。
-
引用がそれを言っている理由を説明できますか?
これは、
time
の場合だけでなく、予約語と
コマンドの違いによるものですか。 ?たとえば、bashシェルは
それらをどのように異なる方法で解析または解釈しますか?または、これは
time
の場合にのみ限定されますか ? -
次の例では、
外部の
time
はなぜですか シェルビルトインと
パイプラインで作業しますが、引用では「これらの時間を簡単に計ることはできません」と書かれていますか?外部の
時間
シェルビルトイン :$ /usr/bin/time echo hello hello 0.00user 0.00system 0:00.00elapsed 0%CPU (0avgtext+0avgdata 1676maxresident)k 0inputs+0outputs (0major+78minor)pagefaults 0swaps
外部の
時間
パイプライン :$ /usr/bin/time sleep 10 | sleep 5 0.00user 0.00system 0:10.00elapsed 0%CPU (0avgtext+0avgdata 1776maxresident)k 0inputs+0outputs (0major+79minor)pagefaults 0swaps
-
次の例では、外部の
time
がなぜですか シェル
関数 不合格?エラー出力はどういう意味ですか?$ function mytest () { sleep 10; } $ /usr/bin/time mytest /usr/bin/time: cannot run mytest: No such file or directory Command exited with non-zero status 127 0.00user 0.00system 0:00.03elapsed 0%CPU (0avgtext+0avgdata 1252maxresident)k 32inputs+0outputs (0major+30minor)pagefaults 0swaps
-
引用は、シェルビルトイン、
シェル関数、パイプラインのタイミングだけでなく、
コマンドのグループのタイミングにも当てはまるようです。 :$ time { echo hello; sleep 3; echo tim; } hello tim real 0m3.002s user 0m0.000s sys 0m0.000s $ /usr/bin/time { echo hello; sleep 3; echo tim; } bash: syntax error near unexpected token `}'
シェルが「bash:予期しないトークンの近くの構文エラー
}
と言うのはなぜですか コマンド/usr / bin / time
の場合は「」 ?
承認された回答:
bash
で 、 time
は予約語であるため、シェルはそれを独自の方法で解析し、ルールを適用できます。
これがbash
のコードを示しています time
で始まる行の解析 予約語:
static int
time_command_acceptable ()
{
#if defined (COMMAND_TIMING)
int i;
if (posixly_correct && shell_compatibility_level > 41)
{
/* Quick check of the rest of the line to find the next token. If it
begins with a `-', Posix says to not return `time' as the token.
This was interp 267. */
i = shell_input_line_index;
while (i < shell_input_line_len && (shell_input_line[i] == ' ' || shell_input_line[i] == 't'))
i++;
if (shell_input_line[i] == '-')
return 0;
}
switch (last_read_token)
{
case 0:
case ';':
case 'n':
case AND_AND:
case OR_OR:
case '&':
case WHILE:
case DO:
case UNTIL:
case IF:
case THEN:
case ELIF:
case ELSE:
case '{': /* } */
case '(': /* )( */
case ')': /* only valid in case statement */
case BANG: /* ! time pipeline */
case TIME: /* time time pipeline */
case TIMEOPT: /* time -p time pipeline */
case TIMEIGN: /* time -p -- ... */
return 1;
default:
return 0;
}
#else
return 0;
#endif /* COMMAND_TIMING */
}
ほら、 time
他のほとんどのbash
が続く可能性があります 予約語。
外部コマンドの場合、通常のルール{
が適用されました / usr / bin / time
の入力と見なされました 。 }コード> 単独では無効なトークンであり、
bash
エラーを発生させます。
で:
/usr/bin/time echo hello
外部のtime
シェルビルトインのecho
を呼び出さなかった しかし、外部の echo
コマンド。
strace
次のことを確認します:
$ strace -fe execve /usr/bin/time echo 1
execve("/usr/bin/time", ["/usr/bin/time", "echo", "1"], [/* 64 vars */]) = 0
Process 25161 attached
....
[pid 25161] execve("/usr/bin/echo", ["echo", "1"], [/* 64 vars */]) = -1 ENOENT (No such file or directory)
[pid 25161] execve("/bin/echo", ["echo", "1"], [/* 64 vars */]) = 0
1
[pid 25161] +++ exited with 0 +++
....
ここでは外部のtime
PATH
を検索します コマンド実行可能ファイルを見つけるための変数。これは、関数を使用する場合にも説明されます。そのようなファイルやディレクトリはありません mytest
という名前のコマンドがないため PATH
で 。