Linux-I/O重定向和管道

来源:https://www.cnblogs.com/yanjieli/archive/2018/11/07/9916960.html
-Advertisement-
Play Games

Linux I/O重定向 標準輸入(stdin):文件描述符0 標準輸入(stdout):文件描述符1 標準錯誤(stderr):文件描述符2 file descriptors(FD,文件描述符 或 Process I/O channels); 進程使用文件描述符來管理打開的文件 0, 1, and ...


Linux I/O重定向

  • 標準輸入(stdin):文件描述符0
  • 標準輸入(stdout):文件描述符1
  • 標準錯誤(stderr):文件描述符2

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

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

[root@centos7-1 ~]# ls /proc/$$/fd
0  1  2  255

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

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

  • 正確輸出:1>  1>>  等價於 > >>
  • 錯誤輸出:2>  2>>

輸出重定向(覆蓋)

[root@centos7-1 ~]# date 1> date.txt

輸出重定向(追加)

[root@centos7-1 ~]# date >> date.txt

錯誤輸出重定向

[root@centos7-1 ~]# ls /home/ /aaaa >list.txt
ls: 無法訪問/aaaa: 沒有那個文件或目錄
[root@centos7-1 ~]# ls /home/ /aaaa >list.txt 2>err.txt      //重定向到不同的位置

正確和錯誤都輸出到相同位置 &>

[root@centos7-1 ~]# ls /home/ /aaaa &>list.txt          //混合輸出

正確和錯誤都輸出到相同位置 2>&1

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

重定向到空設備/dev/null

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

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

/dev/null 補充

/dev/null:是一個空設備,黑洞,任何文件都可以扔進去,但是看不見

如果/dev/null設備被刪除怎麼辦? rm -f /dev/null

1、手動創建

# mknod -m 666 /dev/null c 1 3 
[root@centos7-1 ~]# ll /dev/null /dev/zero 
crw-rw-rw- 1 root root  1,     3 11月  7 12:23 /dev/null
crw-rw-rw- 1 root root  1,     5 11月  7 12:23 /dev/zero
                    主設備號  從設備號
                      MAJOR   MINOR

主設備號相同:表示為同一種設備類型,也可以認為keme使用的是相同的驅動

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

[root@centos7-1 ~]# ll /dev/null /dev/vda1 /etc/hosts
crw-rw-rw-  1 root root   1, 3 11月  7 12:23 /dev/null
brw-rw----  1 root disk 252, 1 11月  7 12:23 /dev/vda1
-rw-r--r--. 1 root root    172 10月 29 14:38 /etc/hosts
c表示字元設備

普通文件和設備文件的區別:

從錶面上看,普通文件有大小;塊設備文件沒有大小,有主設備號和從設備號。

字元設備和快設備的區別:

字元設備沒有緩存,塊設備有緩存

腳本中使用重定向

案例1:腳本中使用重定向
# vim ping.sh 
#!/usr/bin/bash
ping -c1 172.16.120.254 &>/dev/null
if [ $? -eq 0 ];then
    echo "up.."
else
    echo "down.."
fi
# bash ping.sh


案例2:腳本中使用重定向
# vim ping2.sh 
#!/usr/bin/bash
ping -c1 172.16.120.254 &>/dev/null
if [ $? -eq 0 ];then
    echo "172.16.120.254 up.."  > /up.txt
else
    echo "172.16.120.254 down.." >/down.txt
fi
# bash ping2.sh
示例

輸入重定向

標準輸入: <     等價於   0<

案例1

[root@centos7-1 ~]# mail -s "ssss" alice               //沒有改變輸入的方向,預設鍵盤
111
222
333            
^D
[root@centos7-1 ~]# su - alice
[alice@centos7-1 ~]$ mail
Mail version 8.1 6/6/93.  Type ? for help.
"/var/spool/mail/alice": 1 message 1 new
>N  1 [email protected]  Mon Oct 29 14:09  18/657   "ssss"
& 

[root@centos7-1 ~]# mail -s "test01" alice < /etc/hosts    //輸入重定向,來自於文件

案例2

[root@centos7-1 ~]# grep 'root'                         //沒有改變輸入的方向,預設鍵盤,此時等待輸入...
yang sss
sssrootssss..
sssrootssss..

[root@centos7-1 ~]# grep 'root' < /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

案例3

[root@centos7-1 ~]# dd if=/dev/zero of=/file1.txt bs=1M count=2
[root@centos7-1 ~]# dd </dev/zero >/file2.txt bs=1M count=20

案例4 mysql表結構導入

[root@centos7-1 ~]# mysql -uroot -p123 < bbs.sql

重定向綜合案例

綜合案例1:利用重定向建立多行的文件

[root@centos7-1 ~]# echo "111" > file1.txt
[root@centos7-1 ~]# cat file1.txt 
111

[root@centos7-1 ~]# cat >file2.txt
111
222
333
444
^D
[root@centos7-1 ~]# cat file2.txt 

請問:file2.txt有幾行?


[root@centos7-1 ~]# cat >>file3.txt
aaa
bbb
ccc
ddd
^D
[root@centos7-1 ~]# cat file3.txt 

請問:file3.txt有幾行?

[root@centos7-1 ~]# cat >file4 <<EOF
> 111
> 222
> 333
> EOF
[root@centos7-1 ~]# cat file4
111
222
333
View Code

綜合案例2: 腳本中利用重定向列印消息

[root@centos7-1 ~]# vim yang.sh
#!/usr/bin/bash
cat <<-EOF
+------------------------------------------------+
|                                                |
|               ======================    |
|                 虛擬機基本管理centos           |
|                                              |
|               ======================     |
|               1. 安裝虛擬機                    |             
|               2. 重置所有Linux虛擬機           |
|               3. 重置Windows虛擬機             |
|               4. 重置Windows虛擬機  [完全]     |
|               5. 重置指定的虛擬機              |
|               q. 退出管理程式                  | 
|                                                |
+------------------------------------------------+
EOF
View Code

綜合案例3

[root@centos7-1 ~]# ls; date &>/dev/null          //希望兩條命令輸出都重定向

[root@centos7-1 ~]# ls &>/dev/null; date &>/dev/null

[root@centos7-1 ~]# (ls; date) &>/dev/null

[root@centos7-1 ~]# (while :; do date; sleep 2; done) &    //在後臺運行,但輸出依然在終端顯示

[root@centos7-1 ~]# (while :; do date; sleep 2; done) &>date.txt &
[1] 6595
[root@centos7-1 ~]# tailf date.txt 
Tue Apr 12 22:04:32 CST 2017
Tue Apr 12 22:04:34 CST 2017
Tue Apr 12 22:04:36 CST 2017
Tue Apr 12 22:04:38 CST 2017
Tue Apr 12 22:04:40 CST 2017
Tue Apr 12 22:04:42 CST 2017
Tue Apr 12 22:04:44 CST 2017
Tue Apr 12 22:04:46 CST 2017
Tue Apr 12 22:04:48 CST 2017

[root@centos7-1 ~]# jobs
[1]+  Running                 ( while :; do
    date; sleep 2;
done ) &>date.txt &
[root@centos7-1 ~]# kill %1
[root@centos7-1 ~]# jobs
View Code

進程管道Piping

進程管道

用法:command1 | command2 | command3 |....

[root@centos7-1 ~]# ll /dev/ |less
[root@centos7-1 ~]# ps aux |grep 'sshd'
[root@centos7-1 ~]# rpm -qa |grep 'httpd'    //查詢所有安裝的軟體包,過濾包含httpd的包
[root@centos7-1 ~]# yum list |grep 'httpd'
  • 案例1:將/etc/password中的用戶按UID大小排序
    [root@centos7-1 ~]# sort -t":" -k3 -n /etc/passwd   //以: 分隔,將第三列按字數升序
    [root@centos7-1 ~]# sort -t":" -k3 -n /etc/passwd -r    //逆序
    [root@centos7-1 ~]# sort -t":" -k3 -n /etc/passwd |head
    
    -t 指定欄位分隔符--field-separator
    -k 指定列
    -n 按數值
  • 案例2:統計出最占CPU的5個進程
    [root@centos7-1 ~]# ps aux --sort=-%cpu |head -6
  • 案例3:統計當前/etc/password中用戶使用的shell類型
    [root@centos7-1 ~]# awk -F: '{print $7}' /etc/passwd 
    [root@centos7-1 ~]# awk -F: '{print $7}' /etc/passwd |sort
    [root@centos7-1 ~]# awk -F: '{print $7}' /etc/passwd |sort |uniq
    [root@centos7-1 ~]# awk -F: '{print $7}' /etc/passwd |sort |uniq -c
          2 /bin/bash
          1 /bin/sync
          1 /sbin/halt
         41 /sbin/nologin
          1 /sbin/shutdown
  • 案例4:統計網站的訪問情況 top5
    [root@centos7-1 ~]# ss -an |grep :80 |awk -F":" '{print $8}' |sort |uniq -c
        4334 192.168.0.66
        1338 192.168.10.11
        1482 192.168.10.125
         44 192.168.10.183
       3035 192.168.10.213
        375 192.168.10.35
        362 192.168.10.39
    [root@centos7-1 ~]# ss -an |grep :80 |awk -F":" '{print $8}' |sort |uniq -c |sort -k1 -rn |head -n 20 
  • 案例5:列印當前所有IP
    [root@centos7-1 ~]# ip addr |grep 'inet ' |awk '{print $2}' |awk -F"/" '{print $1}' 
    192.168.122.205
  • 案例6:列印根分區已用空間的百分比(僅列印數字)
    [root@centos7-1 ~]# df -P  |grep '/$' |awk '{print $5}' |awk -F"%" '{print $1}'

tee管道

[root@centos7-1 ~]#  ip addr |grep 'inet ' |tee ip.txt |awk -F"/" '{print $1}' |awk '{print $2}'
127.0.0.1
192.168.122.205
[root@centos7-1 ~]# cat ip.txt 
    inet 127.0.0.1/8 scope host lo
    inet 192.168.122.205/24 brd 192.168.122.255 scope global eth0
[root@centos7-1 ~]# ip addr |grep 'inet ' |tee -a ip.txt |awk -F"/" '{print $1}' |awk '{print $2}'
127.0.0.1
192.168.122.205
[root@centos7-1 ~]# date |tee date.txt
2018年 11月 07日 星期三 14:50:04 CST
[root@centos7-1 ~]# cat date.txt 
2018年 11月 07日 星期三 14:50:04 CST

參數傳遞Xargs

awk sed grep sort uniq less more xargs
xargs: ls cp rm

  • 案例1
    [root@localhost ~]# touch /home/file{1..5}
    
    [root@localhost ~]# vim files.txt 
    /home/file1
    /home/file2
    /home/file3
    /home/file4
    /home/file5
    
    [root@localhost ~]# cat files.txt |ls -l
    [root@localhost ~]# cat files.txt |rm -rvf
    
    cont.
    [root@localhost ~]# cat files.txt |xargs ls -l           
    -rw-r--r--. 1 root root 0 Mar 11 10:35 /home/file1
    -rw-r--r--. 1 root root 0 Mar 11 10:35 /home/file2
    -rw-r--r--. 1 root root 0 Mar 11 10:35 /home/file4
    -rw-r--r--. 1 root root 0 Mar 11 10:35 /home/file5
    
    [root@localhost ~]# cat files.txt |xargs rm -rvf          
    removed ‘/home/file1’
    removed ‘/home/file2’
    removed ‘/home/file4’
    removed ‘/home/file5’
  • 案例2
    [root@localhost ~]# touch /home/file{1..5}
    [root@localhost ~]# cat files.txt |xargs -I {} ls -l {}
    -rw-r--r--. 1 root root 0 Mar 11 10:40 /home/file1
    -rw-r--r--. 1 root root 0 Mar 11 10:40 /home/file2
    -rw-r--r--. 1 root root 0 Mar 11 10:40 /home/file4
    -rw-r--r--. 1 root root 0 Mar 11 10:40 /home/file5
    
    [root@localhost ~]# cat files.txt |xargs -I {} cp -rvf {} /tmp
    ‘/home/file1’ -> ‘/tmp/file1’
    ‘/home/file2’ -> ‘/tmp/file2’
    ‘/home/file4’ -> ‘/tmp/file4’
    ‘/home/file5’ -> ‘/tmp/file5’
    
    [root@localhost ~]# cat files.txt |xargs -I YANG cp -rvf YANG /var/tmp
    ‘/home/file1’ -> ‘/var/tmp/file1’
    ‘/home/file2’ -> ‘/var/tmp/file2’
    ‘/home/file4’ -> ‘/var/tmp/file4’
    ‘/home/file5’ -> ‘/var/tmp/file5’
  • 案例3
    [root@localhost ~]# find /etc -iname "*ifcfg*" |xargs -I {} cp -rf {} /tmp

     

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 五、物理記憶體的管理 在內核初始化完成後,記憶體管理的責任由伙伴系統(高效、高速)承擔。 1、伙伴系統的結構 系統記憶體中的每個物理記憶體頁(頁幀),都對應於一個struct page實例。每個記憶體域都關聯了一個struct zone的實例,其中保存了用於管理伙伴數據的主要數組。 sruct free_ar ...
  • cut 切割,簡單的取列命令。 -d 指定分隔符 -f 數字 取第幾列 -c n-m 取n列到m列字元。 //提取字元,不常用 例如:已知bqh.txt文件里的內容為“I am bqh myqq is 1147076062”現在需要文件中過濾出"bqh"和"1147076062"字元串,如何實現? ...
  • 什麼是Shell變數     在初等數學數學方程式中,我們會經常碰到類似於這樣的方程式: y=x+1 ,等號左右兩邊的 x 和 y 稱之為未知數。在編程裡面它們他們則代表變數名與變數值。     通過該示例,我們可以得出一個結論:簡單來說, 變數是編程中最基本 ...
  • 今天我突然發現我一點也忍受不了在UWP應用、wi10視窗、設置等界面無法使用滑鼠滑輪了。這個bug已經困擾了我差不多一年了,從買了這台筆記本就開始了。而且這個問題在中間的某一次升級系統後,也修複了,但後來卻又開始有了。雖然我為了這個問題已經在百度上百度了很多次了,但沒有一次可以幫到我。百度上的解答只 ...
  • 定義一個變數aaa 實現計算的四種方法: 簡單shell小例子:利用for迴圈列印字元小於7的單詞 ...
  • 1 #!/bin/bash 2 # 功能描述:LAMP自動安裝腳本 3 4 # 初始化 5 if [ "$(cat /etc/system-release | awk '{print $(NF-1)}' | awk -F"." '{print $1}')" -ne 7 ] 6 then 7 echo... ...
  • VPN 為了防止信息泄露,節省搭建專線私有網路成本,在互聯網上構造一個虛擬的網路 TSL/SSL 對http通信進行對稱加密,發送公鑰時採用的公鑰加密方式 ...
  • 一、命令介紹 uptime命令 uptime命令用於查看系統負載信息以及系統運行時間等。 free命令 free命令用於查看當前系統中記憶體使用量信息。 二、實例 uptime命令實例 直接運行 uptime命令 左面第一條信息14:45:40 , 是當前系統時間 第二條信息 up 24min ,指系 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...