雲計算學習路線課程大綱資料:I/O重定向

今天給你們分享一些雲計算學習路線課程大綱資料,這篇文章是關於I/O重定向的一些學習筆記資料,但願能給你們一些幫助:shell

I/O Redirectionvim

====================================================================================bash

標準輸入、標準輸出、標準錯誤學習

輸出重定向及綜合案例雲計算

輸入重定向及結合案例ssr

標準輸入、標準輸出、標準錯誤進程

file descriptors (FD,文件描述符 或 Process I/O channels):ip

進程使用文件描述符來管理打開的文件input

[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:重定向到空設備/dev/null

[root@tianyun ~]# ls /home/ /aaaaaaaaa >list.txt 2>/dev/null //空設備,即將產生的輸出丟掉

[root@tianyun ~]# ls /home/ /aaaaaaaaa &>/dev/null //空設備,即將產生的輸出丟掉

思考題:

cp /etc/passwd /dev/null ???

cp /etc/passwd /etc/passwd1 2>/dev/null ???

若是/dev/null設備被刪除 rm -f /dev/null

  1. 手動建立

mknod -m 666 /dev/null c 1 3

  1. 重啓自動建立

主設備號 從設備號

MAJOR MINOR

主設備號相同: 表示爲同一種設備類型,也能夠認爲kernel使用的是相同的驅動

從設備號:在同一類型設備中的一個序號

date > date.txt

適用於備份計劃任務及查看不一樣機器up與down

普通文件和設備文件:

[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 ???

輸入重定向

標準輸入: < 等價 0<

案例1:

[root@tianyun ~]# mail alice //沒有改變輸入的方向,默認鍵盤

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' //沒有改變輸入的方向,默認鍵盤,此時等待輸入...

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>

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: 利用重定向創建多行的文件

手動執行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建立多行文件

[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

後面課程學習安裝源碼軟件時:

[root@tianyun ~]# (./configure && make && make install) &>/dev/null

擴展點:subshell

==當前shell中執行==

[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中執行==

[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 ~]#

若是不但願某些命令的執行對當前shell環境產生影響,請在subshell中執行!

[root@tianyun ~]# (umask 777; touch file8888)

[root@tianyun ~]# ll file8888

---------- 1 root root 0 Apr 12 22:11 file8888

[root@tianyun ~]# umask

0022

相關文章
相關標籤/搜索