|
今天給大家分享一些云計算學(xué)習(xí)路線課程大綱資料,這篇文章是關(guān)于I/O重定向的一些學(xué)習(xí)筆記資料,希望能給大家一些幫助: I/O Redirection ==================================================================================== 標(biāo)準(zhǔn)輸入、標(biāo)準(zhǔn)輸出、標(biāo)準(zhǔn)錯誤 輸出重定向及綜合案例 輸入重定向及結(jié)合案例 標(biāo)準(zhǔn)輸入、標(biāo)準(zhǔn)輸出、標(biāo)準(zhǔn)錯誤
file descriptors (FD,文件描述符 或 Process I/O channels): 進(jìn)程使用文件描述符來管理打開的文件 [root@tianyun ~]# ls /proc/$$/fd 0 1 2 3 4 0, 1, and 2, known as standard input, standard output, and standard error
輸出重定向 (覆蓋,追加) 正確輸出: 1> 1>> 等價于 > >> 錯誤輸出: 2> 2>> 案例1:輸出重定向(覆蓋) [root@tianyun ~]# date 1> date.txt
案例2:輸出重定向(追加) [root@tianyun ~]# date >> date.txt
案例3:錯誤輸出重定向 [root@tianyun ~]# ls /home/ /aaaaaaaaa >list.txt ls: 無法訪問/aaaaaaaaa: 沒有那個文件或目錄 [root@tianyun ~]# ls /home/ /aaaaaaaaa >list.txt 2>error.txt //重定向到不同的位置
案例4: 正確和錯誤都輸入到相同位置 [root@tianyun ~]# ls /home/ /aaaaaaaaa &>list.txt //混合輸出
案例5:重定向到空設(shè)備/dev/null [root@tianyun ~]# ls /home/ /aaaaaaaaa >list.txt 2>/dev/null //空設(shè)備,即將產(chǎn)生的輸出丟掉 [root@tianyun ~]# ls /home/ /aaaaaaaaa &>/dev/null //空設(shè)備,即將產(chǎn)生的輸出丟掉
思考題: cp /etc/passwd /dev/null ??? cp /etc/passwd /etc/passwd1 2>/dev/null ??? 如果/dev/null設(shè)備被刪除 rm -f /dev/null 1. 手動創(chuàng)建 mknod -m 666 /dev/null c 1 3 2. 重啟自動創(chuàng)建
主設(shè)備號 從設(shè)備號 MAJOR MINOR 主設(shè)備號相同: 表示為同一種設(shè)備類型,也可以認(rèn)為kernel使用的是相同的驅(qū)動 從設(shè)備號:在同一類型設(shè)備中的一個序號 date > date.txt 適用于備份計劃任務(wù)及查看不同機器up與down 普通文件和設(shè)備文件: [root@tianyun ~]# ll /dev/null /dev/sda1 /etc/hosts crw-rw-rw- 1 root root 1, 3 8月 1 06:36 /dev/null brw-rw---- 1 root disk 8, 1 8月 1 06:36 /dev/sda1 -rw-r--r--. 1 root root 158 6月 7 2013 /etc/hos 案例7:腳本中使用重定向 [root@tianyun ~]# vim ping1.sh ping -c1 10.18.40.100 if [ $? -eq 0 ];then echo "10.18.40.100 is up." else echo "10.18.40.100 is down!" fi [root@tianyun ~]# chmod +x ping1.sh [root@tianyun ~]# ./ping1.sh 案例7:腳本中使用重定向 [root@tianyun ~]# vim ping1.sh ping -c1 10.18.40.100 &>/dev/null if [ $? -eq 0 ];then echo "10.18.40.100 is up." else echo "10.18.40.100 is down!" fi 案例8:腳本中使用重定向 [root@tianyun ~]# vim ping2.sh ping -c1 10.18.40.100 &>/dev/null if [ $? -eq 0 ];then echo "10.18.40.100 is up." >>up.txt else echo "10.18.40.100 is down!" >>down.txt fi [root@tianyun ~]# vim ping2.sh [root@tianyun ~]# chmod +x ping1.sh [root@tianyun ~]# ./ping2.sh >new.txt ??? >/etc/passwd ??? >/etc ??? 輸入重定向 標(biāo)準(zhǔn)輸入: < 等價 0< 案例1: [root@tianyun ~]# mail alice //沒有改變輸入的方向,默認(rèn)鍵盤 Subject: hello 1111 2222 3333 . EOT [root@tianyun ~]# su - alice [alice@tianyun ~]$ mail Heirloom Mail version 12.5 7/5/10. Type ? for help. "/var/spool/mail/alice": 1 message 1 new >N 1 root Mon Jul 31 15:16 20/617 "hello" [root@tianyun ~]# mail -s "test01" alice < /etc/hosts //輸入重定向,來自于文件 案例2: [root@tianyun ~]# grep 'root' //沒有改變輸入的方向,默認(rèn)鍵盤,此時等待輸入... yang sss sssrootssss.. sssrootssss.. [root@tianyun ~]# grep 'root' < /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin 案例3: [root@tianyun ~]# dd if=/dev/zero of=/file1.txt bs=1M count=2 [root@tianyun ~]# dd </dev/zero >/file2.txt bs=1M count=20 案例5:at [root@tianyun ~]# at now +5 min at> useradd yang99 at> <EOT> job 1 at Mon Jul 31 15:29:00 2017 [root@tianyun ~]# vim at.txt useradd yang100 useradd yang102 [root@tianyun ~]# at now +2 min <a.txt job 2 at Mon Jul 31 15:27:00 2017 綜合案例1: 利用重定向建立多行的文件 手動執(zhí)行shell命令 [root@tianyun ~]# echo "111" > file1.txt [root@tianyun ~]# cat file1.txt 111 [root@tianyun ~]# cat >file2.txt 111 222 333 444 ^D [root@tianyun ~]# cat file2.txt 請問:file2.txt有幾行? [root@tianyun ~]# cat >>file3.txt aaa bbb ccc ddd ^D [root@tianyun ~]# cat file3.txt 請問:file3.txt有幾行? [root@tianyun ~]# cat >file4 <<EOF > 111 > 222 > 333 > EOF [root@tianyun ~]# cat file4 111 222 333 綜合案例2: 利用重定向建立多行的文件 腳本script創(chuàng)建多行文件 [root@tianyun ~]# vim create_file.sh cat >file200.txt <<EOF 111 222 333 yyy ccc EOF [root@tianyun ~]# bash create_file.sh [root@tianyun ~]# cat file200.txt 111 222 333 yyy ccc 綜合案例3: 腳本中利用重定向打印消息 [root@tianyun ~]# cat create_file.sh cat <<-EOF 111 222 333 yyy ccc EOF [root@tianyun ~]# bash create_file.sh 111 222 333 yyy ccc [root@tianyun ~]# vim yang.sh cat <<-EOF +------------------------------------------------+ | | | ====================== | | 虛擬機基本管理 v4.0 | | by tianyun | | ====================== | | 1. 安裝KVM | | 2. 安裝或重置CentOS-6.8 | | 3. 安裝或重置CentOS-7.3 | | 4. 安裝或重置RHEL-6.4 | | 5. 安裝或重置Windows-7 | | 6. 刪除所有虛擬機 | | q. 退出管理程序 | | | +------------------------------------------------+ EOF 綜合案例4 [root@tianyun ~]# ls; date &>/dev/null //希望兩條命令輸出都重定向 ?? [root@tianyun ~]# ls &>/dev/null; date &>/dev/null [root@tianyun ~]# (ls; date) &>/dev/null [root@tianyun ~]# (while :; do date; sleep 2; done) & //在后臺運行,但輸出依然在前臺終端 [1] 6229 [root@tianyun ~]# 2017年 08月 01日 星期二 10:12:42 CST 2017年 08月 01日 星期二 10:12:44 CST 2017年 08月 01日 星期二 10:12:46 CST 2017年 08月 01日 星期二 10:12:48 CST 2017年 08月 01日 星期二 10:12:50 CST [root@tianyun ~]# (while :; do date; sleep 2; done) &>date.txt & [root@tianyun ~]# tailf /date.txt 2017年 08月 01日 星期二 10:15:29 CST 2017年 08月 01日 星期二 10:15:31 CST 2017年 08月 01日 星期二 10:15:33 CST 2017年 08月 01日 星期二 10:15:35 CST 2017年 08月 01日 星期二 10:15:37 CST 2017年 08月 01日 星期二 10:15:39 CST 2017年 08月 01日 星期二 10:15:41 CST [root@tianyun ~]# jobs [1]+ 運行中 ( while :; do date; sleep 2; done ) &>/date.txt & [root@tianyun ~]# kill %1 [root@tianyun ~]# jobs 后面課程學(xué)習(xí)安裝源碼軟件時: [root@tianyun ~]# (./configure && make && make install) &>/dev/null 擴(kuò)展點:subshell ==當(dāng)前shell中執(zhí)行== [root@tianyun ~]# cd /boot; ls config-3.10.0-514.el7.x86_64 efi grub grub2 initramfs-0-rescue-a024cb8d031d445580a7b5aaf92a9ca0.img initramfs-3.10.0-514.el7.x86_64.img initrd-plymouth.img symvers-3.10.0-514.el7.x86_64.gz System.map-3.10.0-514.el7.x86_64 vmlinuz-0-rescue-a024cb8d031d445580a7b5aaf92a9ca0 vmlinuz-3.10.0-514.el7.x86_64 [root@tianyun boot]# ==在subshell中執(zhí)行== [root@tianyun boot]# cd [root@tianyun ~]# (cd /boot; ls) config-3.10.0-514.el7.x86_64 efi grub grub2 initramfs-0-rescue-a024cb8d031d445580a7b5aaf92a9ca0.img initramfs-3.10.0-514.el7.x86_64.img initrd-plymouth.img symvers-3.10.0-514.el7.x86_64.gz System.map-3.10.0-514.el7.x86_64 vmlinuz-0-rescue-a024cb8d031d445580a7b5aaf92a9ca0 vmlinuz-3.10.0-514.el7.x86_64 [root@tianyun ~]# 如果不希望某些命令的執(zhí)行對當(dāng)前shell環(huán)境產(chǎn)生影響,請在subshell中執(zhí)行! [root@tianyun ~]# (umask 777; touch file8888) [root@tianyun ~]# ll file8888 ---------- 1 root root 0 Apr 12 22:11 file8888 [root@tianyun ~]# umask 0022 |
|
|