數(shù)據(jù)流可以分為2種:
輸入數(shù)據(jù)流:以寫(xiě)文件為例,從鍵盤(pán)輸入的字符就輸入數(shù)據(jù)流
輸出數(shù)據(jù)流:以讀文件為例,將文件內(nèi)容顯示到屏幕上,顯示的內(nèi)容就是輸出字符流
數(shù)量流重定向就是指改變數(shù)據(jù)流輸入的方式或輸出的介質(zhì)。比如,輸入數(shù)據(jù)流可以是一個(gè)文件的內(nèi)容,輸出數(shù)據(jù)流介質(zhì)可以是文件而不單單的屏幕
對(duì)于命令行來(lái)說(shuō)輸入數(shù)據(jù)流主要來(lái)自鍵盤(pán),輸出數(shù)據(jù)流只要介質(zhì)是屏幕。
同時(shí)輸出數(shù)據(jù)流又可分為:
正確輸出
錯(cuò)誤輸出
語(yǔ)法:
輸入數(shù)據(jù)流:使用<(覆蓋)或<<(累加)
正確輸出數(shù)據(jù)流:使用>(覆蓋)或>>(累加)
錯(cuò)誤輸出數(shù)據(jù)流:使用2>(覆蓋)或2>>(累加)
說(shuō)明:如果某些信息不想顯示到屏幕上也不保存到文件或設(shè)備上,可以講輸出數(shù)據(jù)流指向/dev/null
舉例1:正確輸出數(shù)據(jù)流(覆蓋)
[root@localhost ~]# ll > ll.file
[root@localhost ~]# vim ll.file
總計(jì) 225968
-rw------- 1 root root 1377 02-14 10:29 anaconda-ks.cfg
-rw-r--r-- 1 root root 207 03-05 11:00 bashrc-back
……..
舉例2:正確輸出數(shù)據(jù)流(累加)
[root@localhost ~]# ll /root >> ll.file
總計(jì) 225968
-rw------- 1 root root 1377 02-14 10:29 anaconda-ks.cfg
-rw-r--r-- 1 root root 207 03-05 11:00 bashrc-back
……..
總計(jì) 225972
-rw------- 1 root root 1377 02-14 10:29 anaconda-ks.cfg
-rw-r--r-- 1 root root 207 03-05 11:00 bashrc-back
……..
舉例3:正確輸出與錯(cuò)誤輸出數(shù)據(jù)流
[root@localhost ~]# ll /root /root/error
ls: /root/error: 沒(méi)有那個(gè)文件或目錄 =>錯(cuò)誤信息
/root: =>正確信息
總計(jì) 225972
-rw------- 1 root root 1377 02-14 10:29 anaconda-ks.cfg
-rw-r--r-- 1 root root 207 03-05 11:00 bashrc-back
………………..
[root@localhost ~]# ll /root /root/error >right.list 2>error.list
[root@localhost ~]# cat right.list
/root:
總計(jì) 225984
-rw------- 1 root root 1377 02-14 10:29 anaconda-ks.cfg
-rw-r--r-- 1 root root 207 03-05 11:00 bashrc-back
……………..
[root@localhost ~]# cat error.list
ls: /root/error: 沒(méi)有那個(gè)文件或目錄
舉例4:正確與錯(cuò)誤輸出數(shù)據(jù)流寫(xiě)在一個(gè)文件中
[root@localhost ~]# ll /root /root/error >all.list 2>&1
[root@localhost ~]# cat all.list
ls: /root/error: 沒(méi)有那個(gè)文件或目錄
/root:
總計(jì) 225996
-rw-r--r-- 1 root root 45 03-05 13:02 all.list
-rw------- 1 root root 1377 02-14 10:29 anaconda-ks.cfg
………………..
語(yǔ)法:
cmd;cmd:不考慮命令相關(guān)性連續(xù)額的命令執(zhí)行
cmd1&& cmd2:若 cmd1執(zhí)行完畢且正確,則執(zhí)行 cmd2
若 cmd1執(zhí)行錯(cuò)誤則不執(zhí)行 cmd2
cmd1|| cmd2:若 cmd1執(zhí)行完畢且正確,則不執(zhí)行 cmd2
若 cmd1執(zhí)行完畢且為錯(cuò)誤,則執(zhí)行 cmd2