シェル組み込み、関数、パイプ、およびコマンドリストの予約語と「time」コマンドの違いは何ですか?

シェル組み込み、関数、パイプ、およびコマンドリストの予約語と「time」コマンドの違いは何ですか?

Bashリファレンスマニュアルでは、

timeタイミングを許可するために予約語として使用シェル組み込み関数シェル機能管路。外部コマンドtimeでは簡単に時間を測定できません。

  1. 引用文でなぜこのようなことを言うのか説明していただけますか?

    これは単なるケースではなく、予約語と命令語の違いによるものですかtime?たとえば、bashシェルはこれをどのように異なる方法で解析または解釈しますか?

    または、次の場合にのみ該当しますかtime

  2. 次の例では

    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
    

    外部time開口パイプライン:

    $ /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
    
  3. 次の例で外部がオンに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
    
  4. この文は、タイミングシェル組み込み、シェル関数、パイプにのみ適用されるわけではないようです。タイミングにも注意してください一連のコマンド:

    $ 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

ベストアンサー1

からbashtime一つです予約語したがって、シェルはそれを独自の方法で解析してルールを適用できます。

予約語で始まる行を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

external は、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

おすすめ記事