GNU/Linux >> Belajar Linux >  >> Linux

Apa itu Exec 3?

Saya mengerti bahwa exec dapat melakukan pengalihan I/O pada shell saat ini, tetapi saya hanya melihat penggunaan seperti:

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

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

Dari situ saya mengerti bahwa < adalah untuk aliran input, > adalah untuk aliran keluaran. Jadi apa exec 3<&1 lakukan?

PS:Saya menemukan ini dari kode sumber Kelelawar

Jawaban yang Diterima:

Dari 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.

Saya melakukan beberapa debug dengan strace :

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

Untuk 3<&1 :

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

Untuk 3>&1 :

dup2(1, 3)                              = 3

Untuk 2>&1 :

dup2(1, 2)                              = 2

Tampaknya 3<&1 lakukan persis sama dengan 3>&1 , menduplikasi stdout ke deskriptor file 3.


Linux
  1. Apa yang dikembalikan malloc(0)?

  2. Apa artinya 'cd -'?

  3. Apa yang dilakukan 'bash -c'?

  1. Apa itu file .so.2?

  2. Apa yang dilakukan kill -- -0?

  3. Apa yang dilakukan `exec [email protected]`?

  1. Apa yang Dilakukan "lc_all=c"?

  2. Apa Arti "rc" Dalam .bashrc?

  3. Apa yang Digema $? Mengerjakan??