文件描述符
linux下一切皆文件
文件描述符,是內核為了高效管理已經被打開的文件所創建的索引,用於指向被打開的文件,所有執行I/O操作的系統調用都通過文件描述符;
文件描述符是一個簡單的非負整數,用以標明每一個被進程打開的文件,程序剛剛啟動時候,第一個打開的是0,第二個是1,以此類推,也可以理解為是一個文件的身份ID
用戶通過操作系統處理信息的過程中,使用的交互設備文件(鍵盤,鼠標,顯示器)
- 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個文件,標准輸入,標准輸出,標准出錯處理,分別對應012
對文件描述做的操作就是對文件本身的操作,可以直接通過操作文件描述符來修改文件
一個進程可以打開的文件描述符的限制,或者說一個進程可以打開多少文件
[root@VM_0_15_centos ~]# ulimit -n
100001
雖然可以修改,但是只有服務器的內存越大,機器的性能越好時 , 可以修改的值更大
[root@VM_0_15_centos ~]# ulimit -n 100002
[root@VM_0_15_centos ~]# ulimit -n
100002
重定向
輸出重定向
定義:
- 將命令的正常輸出結果保存到指定的文件中,而不是直接顯示在顯示器的屏幕上
語法
- > 文件名 表示將標准輸出的內容,寫入到后面的文件中,如果文件名已經存在,則會覆蓋源文件中的內容
- >> 文件名 表示將標准輸出的內容,追加到后面的文件中,不會清除文件原有內容
查看當前主機的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 ~]#
運算順序
- 從左到右,一個一個執行,從上到下執行