GNU/Linux >> Znalost Linux >  >> Linux

Co dělá Exec 3?

Rozumím tomu exec může provést přesměrování I/O na aktuálním shellu, ale vidím pouze použití jako:

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

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

Z toho jsem pochopil, že < je pro vstupní proud, > je pro výstupní proud. Co tedy dělá exec 3<&1 dělat?

PS:Toto jsem našel ze zdrojového kódu Bats

Přijatá odpověď:

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

Provedl jsem nějaké ladění pomocí strace :

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

Pro 3<&1 :

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

Pro 3>&1 :

dup2(1, 3)                              = 3

Pro 2>&1 :

dup2(1, 2)                              = 2

Zdá se, že 3<&1 proveďte přesně to samé jako 3>&1 , duplikující stdout do deskriptoru souboru 3.


Linux
  1. Co dělá 'bash -c'?

  2. Co je soubor .so.2?

  3. Co je to `S_ISREG()` a co dělá?

  1. Co je soubor .so?

  2. Co znamená „rc“ v „.bashrc“ atd.?

  3. Co dělá zabít -- -0?

  1. Co dělá „lc_all=c“?

  2. Co znamená „rc“ v .bashrc?

  3. Co dělá Echo $? Dělat??