[20201116]bash shell IO重定向.txt
[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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- [20180930]bash shell &.txt
- [20210908]Reverse Shell with Bash.txt
- [20210913]bash shell $* and $@ 的區別.txt
- [20181212]bash shell 字串 補零.txt字串
- [20231123]函式與bash shell呼叫.txt函式
- [20230314]nc reverse bash shell alias.txt
- [20230310]nc reverse bash shell問題.txt
- [20201109]bash shell特殊算術方式.txt
- [20210324]bash shell value too great for base.txt
- [20181229]bash shell的算術運算 .txt
- [20230309]nc reverse bash shell or cmd.exe(windows).txtWindows
- [20210618]記錄bash shell執行的命令.txt
- [20210330]bash使用source or ..呼叫shell指令碼注意txt指令碼
- [20231023]生成bbed的執行指令碼(bash shell).txt指令碼
- [20231109]bash shell快捷鍵alt+number的問題.txt
- [20231029]使用cygwin調式bash shell引出的問題.txt
- [20231102]除錯bash shell指令碼遇到的問題.txt除錯指令碼
- [20210906]沒有想到bash shell還可以這樣寫.txt
- [20210107]編寫bash shell指令碼遇到的問題.txt指令碼
- [20190126]從sqlplus執行結果返回bash shell變數.txtSQL變數
- shell Bash變數變數
- 『忘了再學』Shell基礎 — 6、Bash基本功能(輸入輸出重定向)
- [20210318]bash test (( )) [[ ]].txt
- [20181203]bash here $.txt
- [20201116]測試CURSOR_SPACE_FOR_TIME=false(11g).txtFalse
- Bash 單命令列解釋(3)--重定向命令列
- [20190312]bash IFS例子.txt
- Shell輸入\輸出重定向
- [20201116]測試CURSOR_SPACE_FOR_TIME(10g)(補充).txt
- Shell(Bash)學習· 總章
- [20221104]bash exec使用技巧.txt
- [20210207]bash history小技巧.txt
- [20180413]bash 位置引數.txt
- [20180926]bash與分號.txt
- bash shell多執行緒方案執行緒
- Linux深入探索04-Bash shellLinux
- bash shell 程式與磁碟資料
- shell程式設計–bash變數程式設計變數