linux重定向 null和zero

文件描述符

linux下一切皆文件mysql

文件描述符,是內核爲了高效管理已經被打開的文件所建立的索引,用於指向被打開的文件,全部執行I/O操做的系統調用都經過文件描述符;linux

文件描述符是一個簡單的非負整數,用以標明每個被進程打開的文件,程序剛剛啓動時候,第一個打開的是0,第二個是1,以此類推,也能夠理解爲是一個文件的身份IDsql

用戶經過操做系統處理信息的過程當中,使用的交互設備文件(鍵盤,鼠標,顯示器)shell

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

查看一個進程打開了那些文件

1獲取進程號數據庫

[root@VM_0_15_centos ~]# vim /etc/passwd

[root@VM_0_15_centos ~]# ps -aux | grep passwd
root      1476  0.1  0.4 151164  4992 pts/2    S+   22:45   0:00 vim /etc/passwd
root      1534  0.0  0.0 112708   972 pts/1    R+   22:45   0:00 grep --color=auto passwd
[root@VM_0_15_centos ~]# ll /proc/1476/fd
total 0
lrwx------ 1 root root 64 Aug 24 22:46 0 -> /dev/pts/2
lrwx------ 1 root root 64 Aug 24 22:46 1 -> /dev/pts/2
lrwx------ 1 root root 64 Aug 24 22:45 2 -> /dev/pts/2
lrwx------ 1 root root 64 Aug 24 22:46 4 -> /etc/.passwd.swp

一個進程啓動時,都會打開3個文件,標準輸入,標準輸出,標準出錯處理,分別對應012vim

對文件描述作的操做就是對文件自己的操做,能夠直接經過操做文件描述符來修改文件centos

一個進程能夠打開的文件描述符的限制,或者說一個進程能夠打開多少文件bash

[root@VM_0_15_centos ~]# ulimit -n
100001

雖然能夠修改,可是隻有服務器的內存越大,機器的性能越好時 , 能夠修改的值更大服務器

[root@VM_0_15_centos ~]# ulimit -n 100002
[root@VM_0_15_centos ~]# ulimit -n
100002

重定向

輸出重定向

定義:oracle

  • 將命令的正常輸出結果保存到指定的文件中,而不是直接顯示在顯示器的屏幕上

語法

  • > 文件名 表示將標準輸出的內容,寫入到後面的文件中,若是文件名已經存在,則會覆蓋源文件中的內容
  • >> 文件名 表示將標準輸出的內容,追加到後面的文件中,不會清除文件原有內容

查看當前主機的cpu信息,寫入到cpu.txt文件中

[root@VM_0_15_centos ~]# cat /proc/cpuinfo > cpu.txt
[root@VM_0_15_centos ~]# cat cpu.txt 
processor	: 0
vendor_id	: AuthenticAMD
cpu family	: 23
model		: 1
model name	: AMD EPYC Processor
...
bogomips	: 3992.46
TLB size	: 1024 4K pages
clflush size	: 64
cache_alignment	: 64
address sizes	: 48 bits physical, 48 bits virtual
power management:

將內核的版本信息追加到cpu.txt中

...
bogomips	: 3992.46
TLB size	: 1024 4K pages
clflush size	: 64
cache_alignment	: 64
address sizes	: 48 bits physical, 48 bits virtual
power management:

Linux VM_0_15_centos 3.10.0-957.21.3.el7.x86_64 #1 SMP Tue Jun 18 16:35:19 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

清空一個文件,而不是刪除

[root@VM_0_15_centos ~]# > cpu.txt 
[root@VM_0_15_centos ~]# cat cpu.txt 
[root@VM_0_15_centos ~]#

輸入重定向

定義

  • 將接收輸入的途徑由默認的鍵盤改成文件

搜索文件中的某個字符

[root@VM_0_15_centos ~]# uname -a > cpu.txt 
[root@VM_0_15_centos ~]# grep centos < ./cpu.txt 
Linux VM_0_15_centos 3.10.0-957.21.3.el7.x86_64 #1 SMP Tue Jun 18 16:35:19 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

將xuegod.sql導入到mysql數據庫中

mysql -uroot -p 123456 < xuegod.sql

EOF

分界符,形式不是固定的,EOF能夠自定義,可是先後的EOF必須成對出現,且不能和shell命令衝突

1使用eof做爲分界符時,再次輸入eof字符時,會自動退出

[root@VM_0_15_centos ~]# cat >> cpu.txt << eof
> aaa
> bbb
> eof
[root@VM_0_15_centos ~]#

2若是要在文件中輸入eof時,能夠自定義分界符

[root@VM_0_15_centos ~]# cat > cpu.txt << 123
> aaa
> bbb
> eof
> 123
[root@VM_0_15_centos ~]#

3在shell腳本中能夠輸出一組字符,好比菜單

[root@VM_0_15_centos ~]# cat eof.sh 
#!/bin/bash
cat <<eof
======================
1,mysql
2,httpd
3,oracle
======================
eof
[root@VM_0_15_centos ~]# bash ./eof.sh 
======================
1,mysql
2,httpd
3,oracle
======================

錯誤重定向

將命令執行過程當中出現的錯誤信息,(選項或者參數錯誤,)保持到指定的文件,而不是直接顯示到顯示器

  • 操做符: 2>

標準輸出 1>,簡寫 > 標準輸入 1<,簡寫 < 標準錯誤 2>,不能簡寫

  • 在實際應用中,錯誤重定向能夠用來收集執行的錯誤信息,爲排錯提供依據
  • 對於shell腳本還能夠將可有可無的錯誤信息重定向到空文件/dev/null中,以保持腳本輸出的簡潔

1將錯誤顯示的內容和正確顯示的內容分開

[root@VM_0_15_centos ~]# ls /etc/passwd xxxx > a.txt 2> b.txt
[root@VM_0_15_centos ~]# cat a.txt b.txt 
/etc/passwd
ls: cannot access xxxx: No such file or directory

null黑洞和zero空文件

/dev/null

被看作黑洞,全部寫入它的內容都會永久丟失.而嘗試從它那兒讀取內容則什麼也讀不到, 然而/dev/null對命令行和腳本都很是有用

[root@VM_0_15_centos ~]# echo aaa >> /dev/null 
[root@VM_0_15_centos ~]# cat /dev/null 
[root@VM_0_15_centos ~]#

/dev/zero

在類UNIX操做系統中,/dev/zero是一個特殊的文件,當你讀它的時候,她會提供無限的空間符 典型用法是用它來產生一個特定大小的空白文件

使用dd命令產生一個50M的文件

[root@VM_0_15_centos ~]# dd if=/dev/zero of=c.txt bs=1M count=50
50+0 records in
50+0 records out
52428800 bytes (52 MB) copied, 0.0676305 s, 775 MB/s

[root@VM_0_15_centos ~]# ll -h c.txt 
-rw-r--r-- 1 root root 50M Aug 24 23:16 c.txt
  • if表明輸入文件.若是不指定if,默認就會從stdin中讀取輸入
  • of表明輸入文件.若是不指定of,默認就會將stdout做爲默認輸出
  • bs表明字節爲單位的塊大小
  • count表明被複制的塊數

&>和>&符號

&表示等同於的意思

1把正確和錯誤的消息輸入到相同的位置

  • 1>&2 把標準輸出等同於標準錯誤
  • 2>&1 把標準錯誤等同於標準輸出
[root@VM_0_15_centos ~]# ls /tmp/ xxx > 1.txt 2>&1
[root@VM_0_15_centos ~]# cat 1.txt 
ls: cannot access xxx: No such file or directory
/tmp/:
passwd.txt
systemd-private-bf711b8563ea4002ae117d6ff54122cc-ntpd.service-lWhZaP

2把正確和錯誤的消息輸入到相同的位置,能夠簡寫爲

[root@VM_0_15_centos ~]# ls /tmp/ xxx &> 2.txt
[root@VM_0_15_centos ~]# cat 2.txt 
ls: cannot access xxx: No such file or directory
/tmp/:
passwd.txt
systemd-private-bf711b8563ea4002ae117d6ff54122cc-ntpd.service-lWhZaP

3工做中shell腳本中的, >/dev/null 2>&1

  • 解釋,將標準錯誤等同於標準輸出,將標準輸出,輸出到黑洞文件中 將產生的信息丟棄

命令判斷

三個特殊符號

; 分號

  • 不考慮命令的相關性,會連續執行
[root@VM_0_15_centos ~]# ls /aaa /bbb ;echo aaa
ls: cannot access /aaa: No such file or directory
ls: cannot access /bbb: No such file or directory
aaa

&& 邏輯與

  • 它只有在前面的命令執行成功後,後面的命令纔會去執行

若是/opt目錄存在,則在/opt下面新建一個文件a.txt

[root@VM_0_15_centos ~]# cd /opt/ && touch a.txt
[root@VM_0_15_centos opt]# ls /opt/a.txt 
/opt/a.txt

源碼編譯經典實用方法

./configure && make -j 4 && make install

|| 邏輯或

  • 若是前面的命令執行成功,後面的內容不會執行
  • 若是前面的命令執行失敗,會執行後面的內容
[root@VM_0_15_centos ~]# ls xxx || cd /home/
ls: cannot access xxx: No such file or directory
[root@VM_0_15_centos home]#
[root@VM_0_15_centos home]# cd /root/ || echo aaa
[root@VM_0_15_centos ~]#

運算順序

  • 從左到右,一個一個執行,從上到下執行
相關文章
相關標籤/搜索