一、標準I/O ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
標準輸入: 也可以叫STDIN,用0來標識,通常是鍵盤
標準輸出: 也可以叫STDOUT,用1來標識,通常是顯示器
標準錯誤輸出 :STDERR,用2來標識,通常是顯示器
二、I/0重定向 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
I/O重定向是指改變數據的輸入或輸出來源。
1、輸入重定向:<
[root@hao ~]# tr ' a-z ' ' A-Z ' < /etc/ fstab # # /ETC/ FSTAB # CREATED BY ANACONDA ON SAT JUL 26 20 : 12 : 53 2014 # ...
2、在此處生成文檔:<< 通常和EOF或END一起使用
[root@hao ~]# cat << END > the first line > second > end > END the first line second end
[root@hao ~]# cat >> /tmp/myfile.txt << EOF > the first line > second > EOF [root@hao ~]# cat /tmp/ myfile.txt the first line second
3、輸出重定向:> 覆蓋輸出。
會覆蓋目標文件中的內容,容易發生錯誤??梢允褂胹et -C禁止覆蓋已經存在的文件。同理set +C則可以關閉上述功能。默認情況下是可以覆蓋,當然在set -C 關閉覆蓋輸出功能情況下,如果要強制覆蓋輸出,則可以使用>|來強制覆蓋輸出。
set -C
4、輸出重定向:>>追加輸出
[root@hao tmp]# ls / var account crash db games lib lock mail opt run tmp yp cache cvs empty gdm local log nis preserve spool www [root@hao tmp]# ls /var >/tmp/ var.out [root@hao tmp]# cat /tmp/ var.out account cache ...
5、重定向錯誤輸出:2>,如果不是錯誤輸出,則2>相當于>
6、追加方式重定向錯誤輸出:2>>
[root@hao ~]# ls /varr > /tmp/ var2.out ls : cannot access /varr: No such file or directory [root@hao ~]# ls /varr 2 > /tmp/ var2.out [root@hao ~]# cat /tmp/ var2.out ls : cannot access /varr: No such file or directory [root@hao ~]# ls /var 2 > /tmp/ var2.out account crash db games lib lock mail opt run tmp yp cache cvs empty gdm local log nis preserve spool www
7、若為標準輸出,則輸出到某一個文件,若為錯誤輸出,則重定向到另一個文件
[root@hao ~]# ls /var > /tmp/var2.out 2 >/tmp/ err.out [root@hao ~]# cat /tmp/ var2.out account cache crash ... [root@hao ~]# cat /tmp/err.out
8、重定向標準輸出和錯誤輸出至同一個文件:&>
[root@hao ~]# ls /var# &> /tmp/ var3.out [root@hao ~]# cat /tmp/ var3.out ls : cannot access /var#: No such file or directory [root@hao ~]# ls /var &> /tmp/ var3.out [root@hao ~]# cat /tmp/ var3.out account cache ...
三、管道 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
管道:把前一個命令的輸出,作為后一個命令的輸入,以此類推至多個命令。
[root@hao ~]# echo ' hello world ' | tr ' a-z ' ' A-Z ' HELLO WORLD [root@hao ~]# cut -d: -f1 /etc/ passwd | sort abrt adm apache ... [root@hao ~]# cut -d: -f3 /etc/ passwd | sort - n 0 1 2 3 4 5 6 7 8 ... [root@hao ~]# cut -d: -f1 /etc/ passwd | sort | tr ' a-z ' ' A-Z ' ABRT ADM APACHE AVAHI - AUTOIPD BIN ...
四、tee命令,輸出到文件中,且輸出到屏幕上 ? ? ? ? ? ? ? ?
[root@hao ~]# echo ' hello world ' | tee /tmp/ hello.out hello world [root@hao ~]# cat /tmp/ hello.out hello world
五、練習 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
1、統計/usr/bin/目錄下的文件個數
[root@hao ~]# ls /usr/bin | wc - l 1479
2、取出當前系統上所有用戶的shell,要求每種shell只顯示以此,并且按順序顯示
[root@hao ~]# cut -d: -f7 /etc/ passwd | sort - u /bin/ bash /bin/ sync /bin/ tcsh /sbin/ halt /sbin/ nologin /sbin/shutdown
3、顯示/var/log目錄下每個文件的內容類型
[root@hao ~]# file /var/log /* /var/log/anaconda.ifcfg.log: ASCII text /var/log/anaconda.log: UTF-8 Unicode English text /var/log/anaconda.program.log: ASCII English text, with very long lines, with overstriking /var/log/anaconda.storage.log: UTF-8 Unicode C++ program text, with very long lines
...
4、取出/etc/inittab文件的第六行
[root@hao log]# head - 6 /etc/inittab | tail - 1 #
5、取出/etc/passwd文件中倒數第9個用戶的用戶名和shell,顯示到屏幕上并將其保存至/tmp/users文件中
[root@hao log]# tail - 9 /etc/ passwd | head - 1 | cut -d: -f1, 7 | tee /tmp/ users tcpdump: /sbin/nologin
6、顯示/etc目錄下所有一pa開頭的文件,并統計其個數
[root@hao log]# ls -d /etc/pa*| wc - l 5
7、不使用文本編輯器,將alias cls=clear 一行內容添加至當前用戶的.bashrc文件中。
[root@hao log]# echo " alias cls=clear " >> ~/.bashrc
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
