[20201116]bash shell IO重定向.txt

lfree發表於2020-11-16

[20201116]bash shell IO重定向.txt

--//bash shell的檔案控制程式碼預設0,1,2指向標準輸入,標準輸出,標準錯誤,簡單探究IO 重定向問題。
--//測試例子來源:Wiley Shell Scripting.pdf

$ ls -l /dev/std*
lrwxrwxrwx 1 root root 15 2019-12-23 23:46:09 /dev/stderr -> /proc/self/fd/2
lrwxrwxrwx 1 root root 15 2019-12-23 23:46:09 /dev/stdin -> /proc/self/fd/0
lrwxrwxrwx 1 root root 15 2019-12-23 23:46:09 /dev/stdout -> /proc/self/fd/1

1.測試1:
$ ls -l /proc/self/fd
total 0
lrwx------ 1 oracle oinstall 64 2020-11-16 09:56:42 0 -> /dev/pts/4
lrwx------ 1 oracle oinstall 64 2020-11-16 09:56:42 1 -> /dev/pts/4
lrwx------ 1 oracle oinstall 64 2020-11-16 09:56:42 2 -> /dev/pts/4
lr-x------ 1 oracle oinstall 64 2020-11-16 09:56:42 3 -> /proc/50312/fd

2.測試2:
$ ls -l /proc/self/fd > /tmp/ls-output.txt
$ cat /tmp/ls-output.txt
total 0
lrwx------ 1 oracle oinstall 64 2020-11-16 09:57:01 0 -> /dev/pts/4
l-wx------ 1 oracle oinstall 64 2020-11-16 09:57:01 1 -> /tmp/ls-output.txt
lrwx------ 1 oracle oinstall 64 2020-11-16 09:57:01 2 -> /dev/pts/4
lr-x------ 1 oracle oinstall 64 2020-11-16 09:57:01 3 -> /proc/50313/fd
--//標準輸出1定向到/tmp/ls-output.txt。

3.測試3:
$ ls -l /proc/self/fd >| /tmp/ls-output.txt 2>| /tmp/ls-err.txt
$ cat /tmp/ls-output.txt
total 0
lrwx------ 1 oracle oinstall 64 2020-11-16 09:57:57 0 -> /dev/pts/4
l-wx------ 1 oracle oinstall 64 2020-11-16 09:57:57 1 -> /tmp/ls-output.txt
l-wx------ 1 oracle oinstall 64 2020-11-16 09:57:57 2 -> /tmp/ls-err.txt
lr-x------ 1 oracle oinstall 64 2020-11-16 09:57:57 3 -> /proc/50317/fd
--//標準輸出1定向到/tmp/ls-output.txt,標準錯誤2定向到/tmp/ls-err.txt

4.測試4:
$ ls -l /proc/self/fd /nosuchfile >| /tmp/ls-output.txt 2>| /tmp/ls-err.txt

$ cat /tmp/ls-output.txt
/proc/self/fd:
total 0
lrwx------ 1 oracle oinstall 64 2020-11-16 09:59:18 0 -> /dev/pts/4
l-wx------ 1 oracle oinstall 64 2020-11-16 09:59:18 1 -> /tmp/ls-output.txt
l-wx------ 1 oracle oinstall 64 2020-11-16 09:59:18 2 -> /tmp/ls-err.txt
lr-x------ 1 oracle oinstall 64 2020-11-16 09:59:18 3 -> /proc/50335/fd

$ cat /tmp/ls-err.txt
ls: /nosuchfile: No such file or directory

5.測試5:
 $ ls -l /proc/self/fd /nosuchfile < /etc/hosts   >| /tmp/ls-output.txt 2>| /tmp/ls-err.txt
$ cat /tmp/ls-output.txt
/proc/self/fd:
total 0
lr-x------ 1 oracle oinstall 64 2020-11-16 10:00:20 0 -> /etc/hosts
l-wx------ 1 oracle oinstall 64 2020-11-16 10:00:20 1 -> /tmp/ls-output.txt
l-wx------ 1 oracle oinstall 64 2020-11-16 10:00:20 2 -> /tmp/ls-err.txt
lr-x------ 1 oracle oinstall 64 2020-11-16 10:00:20 3 -> /proc/50365/fd
--//標準輸出1定向到/tmp/ls-output.txt,標準錯誤2定向到/tmp/ls-err.txt
--//標準輸入0定向到/etc/hosts

$ cat /tmp/ls-err.txt
ls: /nosuchfile: No such file or directory

6.測試6 exec:

$ echo $$
38978
$ ps -fp 38978
UID        PID  PPID  C STIME TTY          TIME CMD
oracle   38978 38977  0 Apr27 pts/4    00:00:01 -bash
--//程式號38978就是登入執行的bash shell。

$ ls -l /proc/$$/fd
total 0
lrwx------ 1 oracle oinstall 64 2020-04-27 11:27:43 0 -> /dev/pts/4
lrwx------ 1 oracle oinstall 64 2020-04-27 11:27:43 1 -> /dev/pts/4
lrwx------ 1 oracle oinstall 64 2020-04-27 11:27:43 2 -> /dev/pts/4
lrwx------ 1 oracle oinstall 64 2020-11-02 09:26:00 255 -> /dev/pts/4

The open files are 0 (stdin), 1 (stdout), and 2 (stderr). 255 is a little trick that bash uses to keep a
copy of these for when they are redirected. This is specific to bash. Other shells will act the same for
these tests, but will not have file descriptor 255.

7.測試7 exec>:
$ exec 3> /tmp/testing
$ exec 3> /tmp/testing
$ ls -l /proc/$$/fd
total 0
lrwx------ 1 oracle oinstall 64 2020-04-27 11:27:43 0 -> /dev/pts/4
lrwx------ 1 oracle oinstall 64 2020-04-27 11:27:43 1 -> /dev/pts/4
lrwx------ 1 oracle oinstall 64 2020-04-27 11:27:43 2 -> /dev/pts/4
lrwx------ 1 oracle oinstall 64 2020-11-02 09:26:00 255 -> /dev/pts/4
lrwx------ 1 oracle oinstall 64 2020-04-27 11:27:43 3 -> /tmp/testing

8.測試8 exec<:

$ exec 4< /tmp/testing
$ ls -l /proc/$$/fd
total 0
lrwx------ 1 oracle oinstall 64 2020-04-27 11:27:43 0 -> /dev/pts/4
lrwx------ 1 oracle oinstall 64 2020-04-27 11:27:43 1 -> /dev/pts/4
lrwx------ 1 oracle oinstall 64 2020-04-27 11:27:43 2 -> /dev/pts/4
lrwx------ 1 oracle oinstall 64 2020-11-02 09:26:00 255 -> /dev/pts/4
lrwx------ 1 oracle oinstall 64 2020-04-27 11:27:43 3 -> /tmp/testing
lrwx------ 1 oracle oinstall 64 2020-04-27 11:27:43 4 -> /tmp/testing

8.測試9:

$ exec 3<&-
$ ls -l /proc/$$/fd
total 0
lrwx------ 1 oracle oinstall 64 2020-04-27 11:27:43 0 -> /dev/pts/4
lrwx------ 1 oracle oinstall 64 2020-04-27 11:27:43 1 -> /dev/pts/4
lrwx------ 1 oracle oinstall 64 2020-04-27 11:27:43 2 -> /dev/pts/4
lrwx------ 1 oracle oinstall 64 2020-11-02 09:26:00 255 -> /dev/pts/4
lrwx------ 1 oracle oinstall 64 2020-04-27 11:27:43 4 -> /tmp/testing

$ exec 4<&-
$ ls -l /proc/$$/fd
total 0
lrwx------ 1 oracle oinstall 64 2020-04-27 11:27:43 0 -> /dev/pts/4
lrwx------ 1 oracle oinstall 64 2020-04-27 11:27:43 1 -> /dev/pts/4
lrwx------ 1 oracle oinstall 64 2020-04-27 11:27:43 2 -> /dev/pts/4
lrwx------ 1 oracle oinstall 64 2020-11-02 09:26:00 255 -> /dev/pts/4

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2734342/,如需轉載,請註明出處,否則將追究法律責任。

相關文章