- > :以覆蓋的方法將『正確的資料』輸出到指定的檔案或裝置上
- 1> :以覆蓋的方法將『正確的資料』輸出到指定的檔案或裝置上
- 1>>:以累加的方法將『正確的資料』輸出到指定的檔案或裝置上
- 2> :以覆蓋的方法將『錯誤的資料』輸出到指定的檔案或裝置上
- 2>>:以累加的方法將『錯誤的資料』輸出到指定的檔案或裝置上
- <:將原本需要由鍵盤輸入的資料,改由檔案內容來取代
- <<:代表的是『結束的輸入字元』
例如:『我要用 cat 直接將輸入的資訊輸出到 catfile 中, 且當由鍵盤輸入 eof 時,該次輸入就結束』,那我可以這樣做:
[root@www ~]# cat > catfile << "eof"> This is a test.> OK now stop> eof <==輸入這關鍵詞,立刻就結束而不需要輸入 [ctrl]+d[root@www ~]# cat catfileThis is a test.OK now stop
其中eof可以使用其他的任一字元串代替。還有一個“<<-”,主要是為了在輸出是忽略掉語句前面的一個或者多個tab符,看下面的例子:
#!/bin/bashcat << EOF123456EOF
輸出為:
123456如果改成:#!/bin/bashcat <<- EOF123456EOF
輸出為:
123456
- 2>& 1:將正確和錯誤的資料全部寫入到指定檔案或裝置上
例如:
yan@yan-vm:~$ ll /root/ /home/ > result 2>& 1yan@yan-vm:~$ cat result/home/:total 16drwxr-xr-x 4 root root 4096 Jun 4 13:03 ./drwxr-xr-x 23 root root 4096 Apr 12 20:43 ../drwx------ 2 normaluser normalgroup 4096 Jun 4 13:14 normaluser/drwxr-xr-x 24 yan yan 4096 Jun 5 06:36 yan/ls: cannot open directory /root/: Permission denied
使用者yan無法訪問/root,所以Permission denied是錯誤資料,其他的都是正確的資料。
下面我們來衍生幾種用法,大家猜猜看運行結果是什麼:
ll /root/ /home/ > result 1>&2
ll /root/ /home/ 2> result 1>&2
ll /root/ /home/ 2>&1 > result
結果:
yan@yan-vm:~$ ll /root/ /home/ > result 1>&2/home/:total 16drwxr-xr-x 4 root root 4096 Jun 4 13:03 ./drwxr-xr-x 23 root root 4096 Apr 12 20:43 ../drwx------ 2 normaluser normalgroup 4096 Jun 4 13:14 normaluser/drwxr-xr-x 24 yan yan 4096 Jun 5 06:36 yan/ls: cannot open directory /root/: Permission deniedyan@yan-vm:~$ cat resultyan@yan-vm:~$
分析:正確的資料合併到錯誤的資料,將正確的資料寫到result中,但是此時正確的資料為空白,所以寫入到result檔案中為空白,
將錯誤的資料(包含正確的資料)顯示到螢幕上。
yan@yan-vm:~$ ll /root/ /home/ 2> result 1>&2yan@yan-vm:~$ cat result/home/:total 16drwxr-xr-x 4 root root 4096 Jun 4 13:03 ./drwxr-xr-x 23 root root 4096 Apr 12 20:43 ../drwx------ 2 normaluser normalgroup 4096 Jun 4 13:14 normaluser/drwxr-xr-x 24 yan yan 4096 Jun 5 06:36 yan/ls: cannot open directory /root/: Permission denied
分析:正確的資料合併到錯誤的資料,將錯誤的資料寫到result中,此時錯誤的資料中包含正確的資料,所以result中包含正確和錯誤的資料。
將正確的資料寫到螢幕上,但是此時正確的資料為空白,所以沒有任何資訊顯示在螢幕上。
yan@yan-vm:~$ ll /root/ /home/ 2>&1 > resultls: cannot open directory /root/: Permission deniedyan@yan-vm:~$ cat result/home/:total 16drwxr-xr-x 4 root root 4096 Jun 4 13:03 ./drwxr-xr-x 23 root root 4096 Apr 12 20:43 ../drwx------ 2 normaluser normalgroup 4096 Jun 4 13:14 normaluser/drwxr-xr-x 24 yan yan 4096 Jun 5 06:36 yan/yan@yan-vm:~$
分析:沒有這種用法,所以2>&1被忽略。