天天看點

Linux重定向與管道技術一、重定向基礎概述二、程序中管道技術

文章目錄

  • 一、重定向基礎概述
    • 1. 什麼是重定向
    • 2.為什麼需要重定向
    • 3. 标準輸入與輸出
    • 4. 輸入重定向
  • 二、程序中管道技術
    • 1.什麼是管道
    • 2.管道流程示意圖
    • 2. tee與xargs
    • 3. 管道中使用xargs

一、重定向基礎概述

1. 什麼是重定向

将原本要輸出到螢幕中的資料資訊,重新定向到某個指定的檔案中,或者定向到黑洞(/dev/null)中。

2.為什麼需要重定向

  1. 當程式執行輸出的資訊比較多時,需要儲存下來在進行分頁檢視
  2. 背景執行的程式一般都會有輸出,不希望它的輸出幹擾到螢幕
  3. 定時執行的備份任務,希望将備份的結果保留下來時
  4. 希望将錯誤日志與正确日志,分别輸出儲存到不同檔案時

3. 标準輸入與輸出

當程序操作一個檔案時,首先程序是無法直接操作檔案的需要通過核心來通路檔案,而核心 kernel 需要利用檔案描述符 (file descriptor) 來通路檔案。

程序-------》檔案描述符(非負整數)------》通路-----》檔案名稱,程序通過檔案描述符來管理打開檔案的對應關系。

tail -f /etc/passwd 
過濾 tail 檢視程序号
[[email protected] fd]# ps aux |grep tail
root       4422  0.0  0.0 108092   616 pts/2    S+   12:23   0:00 tail -f /etc/passwd
root       4429  0.0  0.0 112808   964 pts/1    R+   12:24   0:00 grep --color=auto tail

顯示檔案 
ll /etc/4422/fd
total 0
lrwx------. 1 root root 64 Jul 28 12:24 0 -> /dev/pts/2
lrwx------. 1 root root 64 Jul 28 12:24 1 -> /dev/pts/2
lrwx------. 1 root root 64 Jul 28 12:23 2 -> /dev/pts/2
lr-x------. 1 root root 64 Jul 28 12:24 3 -> /etc/passwd
lr-x------. 1 root root 64 Jul 28 12:24 4 -> anon_inode:inotify

           

從上面可以看出通過檔案描述符3 --> /etc/passwd --> inode --> block

0:代表标準輸入

1:代表标準正确輸出

2:代表标準錯誤輸出

通常程式通路一個檔案至少會打開三個标準檔案,分别是标準輸入、标準輸出、錯誤輸出

程序将從标準輸入中得到資料,将正常輸出列印至螢幕終端,将錯誤的輸出資訊也列印至螢幕終端。

Linux重定向與管道技術一、重定向基礎概述二、程式中管道技術
Linux重定向與管道技術一、重定向基礎概述二、程式中管道技術

1. 标準輸出重定向

Linux重定向與管道技術一、重定向基礎概述二、程式中管道技術

1.如果檔案不存在則建立

2.如果檔案存在則清空内容

[[email protected] ~]# >test.txt
[[email protected] ~]# ifconfig eth32 > test.txt 
           

2.标準追加輸出重定向

Linux重定向與管道技術一、重定向基礎概述二、程式中管道技術

如果檔案不存在則建立

如果檔案存在則在檔案尾部添加内容

3.錯誤輸出重定向

Linux重定向與管道技術一、重定向基礎概述二、程式中管道技術

正确輸出及錯誤輸出至相同檔案

正确輸出及錯誤輸出至不同的檔案

[[email protected] ~]# find /etc -name "*.conf" 1>ok 2>ok
[[email protected] ~]# find /etc -name "*.conf" 1>ok 2>err
           

4.混合和輸出重定向

Linux重定向與管道技術一、重定向基礎概述二、程式中管道技術

5. 正确和錯誤都輸入到相同位置

Linux重定向與管道技術一、重定向基礎概述二、程式中管道技術
[[email protected] ~]# ls /root >test.txt 2>&1
[[email protected] ~]# ls /root &>test.txt
           

6. 将内容輸出至黑洞

Linux重定向與管道技術一、重定向基礎概述二、程式中管道技術
[[email protected] ~]# ls /root &>/dev/null
           

4. 輸入重定向

輸入重定向:指的是 ”重新指定裝置“ 來 “代替鍵盤” 作為新的輸入裝置

1.通過輸入重定向讀取檔案内容

[[email protected] ~]# cat < /etc/hosts# cat < /etc/hosts
           

2.通過輸入重定向讀入多行内容

[[email protected] ~]# cat >a.txt <<EOF
> 123
> 456
> 789
> EOF  # 結束标志
[[email protected] ~]# cat a.txt 
123
456
789
[[email protected] ~]# 

           

腳本輸入重定向

使用輸入重定向列印安裝服務的菜單導航欄

[[email protected] ~]# cat install.sh
#!/usr/bin/bash
cat <<-EOF 
---------主菜單---------- 
|   1) 安裝nginx      | 
|   2) 安裝php        |
|   3) 退出           |
 --------------------------
 EOF
read -p "請選擇你要安裝的軟體編号 [ 1 | 2 | 3 ]: 
           

輸入重定向場景:

1.恢複資料時

2.腳本列印菜單時會用到

3.cat寫入檔案時

混合輸出

&>/dev/null
&>>/dev/null
1>/dev/null 2>&1
           
$? :代表上條指令的傳回值(運作傳回值) 當傳回值為0表示上條指令運作成功
& :是标準正确和錯誤輸出
> :覆寫重定向(先清空該檔案,然後寫入新内容)
>> :追加重定向(在原有的檔案後寫入新内容)
< :輸入重定向
 
1.使用grep過濾檔案中包含root的行, 要求使用輸入重定向方式(/etc/passwd)
grep 'root' < /etc/passwd
cat /etc/passwd | grep 'root'
grep 'root' /etc/passwd
2.編寫一個腳本,要求正确輸出到log檔案,錯誤輸出到log1檔案
echo 111 1>log 2>log1
           

二、程序中管道技術

1.什麼是管道

管道操作符号 “|”,主要用來連接配接左右兩個指令, 将左側的指令的【标準輸出】, 交給右側指令的【标準輸入】

注意: 無法傳遞标準錯誤輸出至後者指令

2.管道流程示意圖

格式:

cmd1 | cmd2 [...|cmdn]

Linux重定向與管道技術一、重定向基礎概述二、程式中管道技術

例1. 将 /etc/passwd 中的使用者按 UID 大小排序

[[email protected] ~]# sort -t":" -k3 -n /etc/passwd
[[email protected] ~]# sort -t":" -k3 -n /etc/passwd -r
[[email protected] ~]# sort -t":" -k3 -n /etc/passwd |head -3

[[email protected] ~]# awk -F: '{print $3}' /etc/passwd | sort -n -r | head -3
           

例2. 統計目前 /etc/passwd 中使用者使用的 shell 類型

[[email protected] ~]# awk -F: '{print $NF}' /etc/passwd | sort | uniq -c | sort -n
           

例3. 列印并輸出目前所有主機所有網卡的 IP 位址

[[email protected] ~]# ifconfig ens32 | awk 'NR==2' | awk '{print $2}'
10.0.0.100

[[email protected] ~]# ifconfig ens32 |grep 'inet ' | awk '{print $2}'
10.0.0.100
           

2. tee與xargs

管道中的tee

Linux重定向與管道技術一、重定向基礎概述二、程式中管道技術

tee可以攔截管道過程到指定的地方(檔案、中端等)而并不影響管道的正常傳輸

取ip并把中間結果輸入到檔案

# 輸出到檔案
ip addr |grep 'inet ' |tee -a ip.txt |awk -F"/" '{print $1}' |awk '{print $2}'


# 将tee過程輸出到目前字元裝置
[[email protected] ~]# tty
/dev/pts/1

ip addr |grep 'inet ' |tee /dev/pts/1 |awk -F"/" '{print $1}' |awk '{print $2}

           

3. 管道中使用xargs

xargs 參數傳遞,主要讓一些不支援管道的指令可以使用管道技術

[[email protected] ~]# which cat | ls -l  # 仍然顯示的目前
[[email protected] ~]# which cat |xargs  ls -l
-rwxr-xr-x. 1 root root 54080 Aug 20  2019 /usr/bin/cat


[[email protected] ~]# ls | rm -rf
[[email protected] ~]# ls  # 不支援管道,并沒有删除
anaconda-ks.cfg  a.txt  edu.txt  ip.txt  test.txt
[[email protected] ~]# ls |xargs  rm -rf
[[email protected] ~]# ls
[[email protected] ~]
           
[[email protected] ~]# touch {1..100}
[[email protected] ~]# ls |xargs -n 1 # 可以指定每行顯示的列

[[email protected] ~]# ls | xargs -n 1 | sed -r 's/(.*)/rm -rf \1/' | bash

[[email protected] ~]# ls |xargs -n 1 |xargs  rm -rf 


           

繼續閱讀