GNU/Linux >> Linux の 問題 >  >> Linux

Exec 3とは何ですか?

execを理解しています 現在のシェルでI/Oリダイレクトを実行できますが、次のような使用法しか表示されません:

exec 6<&0   # Link file descriptor #6 with stdin.
            # Saves stdin.

exec 6>&1   # Link file descriptor #6 with stdout.
            # Saves stdout.

それから私は< 入力ストリーム用です、> 出力ストリーム用です。では、exec 3<&1は何をするのでしょうか? しますか?

PS:これはBatsのソースコードから見つけました

承認された回答:

bash manpageから :

Duplicating File Descriptors
       The redirection operator

              [n]<&word

       is used to duplicate input file descriptors.  If word expands to one or
       more  digits,  the file descriptor denoted by n is made to be a copy of
       that file descriptor.  If the digits in word  do  not  specify  a  file
       descriptor  open for input, a redirection error occurs.  If word evalu‐
       ates to -, file descriptor n is closed.  If n  is  not  specified,  the
       standard input (file descriptor 0) is used.

       The operator

              [n]>&word

       is  used  similarly  to duplicate output file descriptors.  If n is not
       specified, the standard output (file descriptor 1)  is  used.   If  the
       digits  in word do not specify a file descriptor open for output, a re‐
       direction error occurs.  As a special case, if n is omitted,  and  word
       does not expand to one or more digits, the standard output and standard
       error are redirected as described previously.

straceでいくつかのデバッグを行いました :

sudo strace -f -s 200 -e trace=dup2 bash redirect.sh

3<&1の場合 :

dup2(3, 255)                            = 255
dup2(1, 3)                              = 3

3>&1の場合 :

dup2(1, 3)                              = 3

2>&1の場合 :

dup2(1, 2)                              = 2

3<&1のようです 3>&1とまったく同じようにします 、stdoutをファイル記述子3に複製します。


Linux
  1. 「lc_all=c」は何をしますか?

  2. 何をしますか?

  3. .bashrcの「rc」は何の略ですか?

  1. rm はどのように機能しますか? rm は何をしますか?

  2. `S_ISREG()` とは何ですか?

  3. kill -- -0 は何をしますか?

  1. malloc(0) は何を返しますか?

  2. .pid ファイルとは何ですか? また、何が含まれていますか?

  3. `exec [email protected]` は何をしますか?