| 1 | #!/usr/bin/env bash |
| 2 | exec 51>1.txt # disable=#D0857 |
| 3 | exec 10>&-; { exec 52<&10; } 51</dev/null; exec 51<&52 # disable=#D2164 |
| 4 | exec 50>&51 # disable=#D0857 |
| 5 | echo "====" |
| 6 | exec 50>2.txt # disable=#D0857 |
| 7 | echo "====" |
| 8 | echo hi >&50 |
| 9 | ls -l 1.txt 2.txt |
| 10 | |
| 11 | # In Linux: |
| 12 | # |
| 13 | # $ strace -e trace=%desc bash D2165.sh |
| 14 | # openat(AT_FDCWD, "1.txt", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3 |
| 15 | # fcntl(51, F_GETFD) = -1 EBADF (不正なファイル記述子です) |
| 16 | # dup2(3, 51) = 51 |
| 17 | # close(3) = 0 |
| 18 | # fcntl(10, F_GETFD) = -1 EBADF (不正なファイル記述子です) |
| 19 | # close(10) = -1 EBADF (不正なファイル記述子です) |
| 20 | # openat(AT_FDCWD, "/dev/null", O_RDONLY) = 3 |
| 21 | # fcntl(51, F_GETFD) = 0 |
| 22 | # fcntl(51, F_DUPFD, 10) = 10 |
| 23 | # fcntl(51, F_GETFD) = 0 |
| 24 | # fcntl(10, F_SETFD, FD_CLOEXEC) = 0 |
| 25 | # dup2(3, 51) = 51 |
| 26 | # close(3) = 0 |
| 27 | # fcntl(52, F_GETFD) = -1 EBADF (不正なファイル記述子です) |
| 28 | # dup2(10, 52) = 52 |
| 29 | # fcntl(10, F_GETFD) = 0x1 (flags FD_CLOEXEC) |
| 30 | # fcntl(52, F_SETFD, FD_CLOEXEC) = 0 |
| 31 | # dup2(10, 51) = 51 |
| 32 | # fcntl(10, F_GETFD) = 0x1 (flags FD_CLOEXEC) |
| 33 | # fcntl(51, F_SETFD, FD_CLOEXEC) = 0 |
| 34 | # fcntl(51, F_SETFD, 0) = 0 |
| 35 | # close(10) = 0 |
| 36 | # fcntl(51, F_GETFD) = 0 |
| 37 | # fcntl(51, F_DUPFD, 53) = 53 |
| 38 | # fcntl(51, F_GETFD) = 0 |
| 39 | # fcntl(53, F_SETFD, FD_CLOEXEC) = 0 |
| 40 | # dup2(52, 51) = 51 |
| 41 | # fcntl(52, F_GETFD) = 0x1 (flags FD_CLOEXEC) |
| 42 | # fcntl(51, F_SETFD, FD_CLOEXEC) = 0 |
| 43 | # close(53) = 0 |
| 44 | # fcntl(50, F_GETFD) = -1 EBADF (不正なファイル記述子です) |
| 45 | # dup2(51, 50) = 50 |
| 46 | # fcntl(51, F_GETFD) = 0x1 (flags FD_CLOEXEC) |
| 47 | # fcntl(50, F_SETFD, FD_CLOEXEC) = 0 |
| 48 | # openat(AT_FDCWD, "2.txt", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3 |
| 49 | # fcntl(50, F_GETFD) = 0x1 (flags FD_CLOEXEC) |
| 50 | # fcntl(50, F_DUPFD, 10) = 10 |
| 51 | # fcntl(50, F_GETFD) = 0x1 (flags FD_CLOEXEC) |
| 52 | # fcntl(10, F_SETFD, FD_CLOEXEC) = 0 |
| 53 | # dup2(3, 50) = 50 |
| 54 | # close(3) = 0 |