tft每日頭條

 > 生活

 > linux中io性能測試

linux中io性能測試

生活 更新时间:2024-10-08 13:33:22

linux中io性能測試(Linux學習的正确姿勢10)1

I/O重定向

标準輸入、标準輸出、标準錯誤

輸出重定向及綜合案例

輸入重定向及結合案例

标準輸入、标準輸出、标準錯誤

linux中io性能測試(Linux學習的正确姿勢10)2

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

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

[root@tianyun ~]# ls /proc/$$/fd

0 1 2 3 4


0, 1, and 2, known as standard input, standard output, and standard error

linux中io性能測試(Linux學習的正确姿勢10)3

輸出重定向 (覆蓋,追加)

正确輸出: 1> 1>> 等價于 > >>

錯誤輸出: 2> 2>>

案例1:輸出重定向(覆蓋)

[root@tianyun ~]# date 1> date.txt

linux中io性能測試(Linux學習的正确姿勢10)4

案例2:輸出重定向(追加)

[root@tianyun ~]# date >> date.txt

linux中io性能測試(Linux學習的正确姿勢10)5

案例3:錯誤輸出重定向

[root@tianyun ~]# ls /home/ /aaaaaaaaa >list.txt

ls: 無法訪問/aaaaaaaaa: 沒有那個文件或目錄

[root@tianyun ~]# ls /home/ /aaaaaaaaa >list.txt 2>error.txt //重定向到不同的位置

linux中io性能測試(Linux學習的正确姿勢10)6

案例4: 正确和錯誤都輸入到相同位置

[root@tianyun ~]# ls /home/ /aaaaaaaaa &>list.txt //混合輸出

linux中io性能測試(Linux學習的正确姿勢10)7

案例5: 正确和錯誤都輸入到相同位置

[root@tianyun ~]# ls /home/ /aaaaaaaaa >list.txt 2>&1 //重定向到相同的位置

linux中io性能測試(Linux學習的正确姿勢10)8

案例6:重定向到空設備/dev/null

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

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

linux中io性能測試(Linux學習的正确姿勢10)9

linux中io性能測試(Linux學習的正确姿勢10)10

linux中io性能測試(Linux學習的正确姿勢10)11

linux中io性能測試(Linux學習的正确姿勢10)12

輸入重定向

标準輸入: < 等價 0<

案例1:

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

Subject: hello

1111

2222

3333

EOF


[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

案例4:mysql表結構導入

[root@tianyun ~]# yum -y install MariaDB-server mariadb

[root@tianyun ~]# systemctl start mariadb

[root@tianyun ~]# vim bbs.sql

create database bbs;

create table bbs.t1 (id int);

insert into bbs.t1 values(1);

[root@tianyun ~]# mysql <bbs.sql

[root@tianyun ~]# mysql

MariaDB [(none)]> show databases;

MariaDB [(none)]> \q


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

手動執行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


如果不希望某些命令的執行對當前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

,

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关生活资讯推荐

热门生活资讯推荐

网友关注

Copyright 2023-2024 - www.tftnews.com All Rights Reserved