歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> SHELL編程 >> shell數據流重定向和管道命令

shell數據流重定向和管道命令

日期:2017/3/1 9:09:20   编辑:SHELL編程

標准輸入輸出和錯誤
標准輸入(stdin) 是指令數據的輸入,代碼為0,使用<或者<<,默認是鍵盤
標准輸出(stdout)是指令執行成功返回的結果,代碼為1,使用>或者>>,默認由屏幕顯示
標准錯誤輸出(stderr)是指令執行失敗返回的錯誤信息,代碼為2,使用2>或者2>>,默認是屏幕

< 指定輸入的數據媒介來源
1> 將正確的內容 覆蓋輸出到指定的媒介
1>> 將正確的內容 追加到指定的媒介
2> 將錯誤信息覆蓋輸出到指定媒介
2>> 將錯誤信息追加輸出到指定媒介

默認只能保存正確的

同時分類導出
[admin@localhost110 ~]$ rm -rf success fail result
[admin@localhost110 ~]$ find /root a.txt 1>success 2>fail
[admin@localhost110 ~]$ cat success
/root
a.txt
[admin@localhost110 ~]$ cat fail
find: “/root”: 權限不夠

正確錯誤的均導入文件
[admin@localhost110 ~]$ find /root a.txt >result 2>&1
[admin@localhost110 ~]$ cat result
/root
find: “/root”: 權限不夠
a.txt

[admin@localhost110 ~]$ find /root a.txt >result1 2>&1
[admin@localhost110 ~]$ find /root a.txt >result
find: “/root”: 權限不夠
[admin@localhost110 ~]$ find /root a.txt &>result2
[admin@localhost110 ~]$

&表示[012]

grep [a-z] -n <a.txt

替換
小寫字母替換成大寫字母
tr [a-z] [A-Z]<a.txt >a1.txt

管道命令使用
命令通過管道符號|連接
能夠接收標准輸入(stdin),如tail/more/grep等
能夠接收來自於前一個指令的數據成為stdin進行處理 只能處理正確的輸出,不能處理錯誤的輸出
ls -8|grep ls
處理不了
grep [-cinv] 'key' filename 支持正則
-c ,計算字符出現的次數
-i,忽略大小寫進行查找
-n,輸出行號
-v,顯示沒有該字符的行
[root@localhost110 ~]# cat log -n
1 php
2 ajax
3 java
4 python
5 nginx mysql
6 GO
7 PHP5
8
[root@localhost110 ~]# grep 'php' log
php
[root@localhost110 ~]# grep -n 'php' log
1:php
[root@localhost110 ~]# grep -ni 'php' log
1:php
7:PHP5
[root@localhost110 ~]# grep -ci 'php' log
2
[root@localhost110 ~]# grep -inv 'php' log
2:ajax
3:java
4:python
5:nginx mysql
6:GO
8:
[root@localhost110 ~]# grep -n [a-z] log
1:php
2:ajax
3:java
4:python
5:nginx mysql
[root@localhost110 ~]# grep -nc [a-z] log
5
grep -n --color [a-z] log

統計當前登錄用戶
[root@localhost110 ~]# w
01:53:08 up 18:15, 1 user, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.1.101 01:19 0.00s 0.24s 0.13s w
[root@localhost110 ~]# w|grep -n root
3:root pts/0 192.168.1.101 01:19 0.00s 0.11s 0.00s w
[root@localhost110 ~]# w|grep -nc root
1
不是root的
[root@localhost110 ~]# w|grep -v root
01:54:20 up 18:16, 1 user, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT

sort指令
sort [-fbknrtu] filename
-f 忽略大小寫
-b 忽略最前面的空格
-M 以月份英文字母排序
-n 使用數字排序
-r 逆向排序
-t 分隔符標識 默認是tab
-k 以第幾列來排序

[root@localhost110 ~]# ls
ab anaconda-ks.cfg a.php b.php composer.phar install.log install.log.syslog log mysql_listen.sh 公共的 模板 視頻 圖片 文檔 下載 音樂 桌面

[root@localhost110 ~]# ls|sort
ab
anaconda-ks.cfg
a.php
b.php
composer.phar
install.log
install.log.syslog
log
mysql_listen.sh
[root@localhost110 ~]# ls|sort -r
mysql_listen.sh
log
install.log.syslog
install.log
composer.phar
b.php
a.php
anaconda-ks.cfg
ab
[root@localhost110 ~]# ls|sort
ab
anaconda-ks.cfg
a.php
A.php
b.php
B.php
composer.phar
D.php
install.log
install.log.syslog
log
mysql_listen.sh
默認不區分大小寫

按照文件大小寫來排序
[root@localhost110 ~]# ls -l|sort -t ' ' -k 5 -n
總用量 1740
-rw-r--r--. 1 root root 6 10月 16 02:17 1
-rw-r--r--. 1 root root 6 10月 16 02:17 B.php
-rw-r--r--. 1 root root 8 10月 16 01:59 a.php
-rw-r--r--. 1 root root 11 10月 16 02:06 A.php
-rw-r--r--. 1 root root 42 10月 16 01:42 log
-rw-r--r--. 1 root root 140 10月 16 02:02 ab
-rw-r--r--. 1 root root 143 10月 16 01:59 b.php
-rwxrwxrwx. 1 root root 272 1月 27 2016 mysql_listen.sh
-rw-r--r--. 1 root root 1112 10月 16 02:17 D.php
-rw-------. 1 root root 1416 1月 13 2016 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4096 1月 13 2016 公共的
drwxr-xr-x. 2 root root 4096 1月 13 2016 模板
drwxr-xr-x. 2 root root 4096 1月 13 2016 視頻
drwxr-xr-x. 2 root root 4096 1月 13 2016 圖片
drwxr-xr-x. 2 root root 4096 1月 13 2016 文檔
drwxr-xr-x. 2 root root 4096 1月 13 2016 下載
drwxr-xr-x. 2 root root 4096 1月 13 2016 音樂
drwxr-xr-x. 2 root root 4096 1月 13 2016 桌面
-rw-r--r--. 1 root root 10033 1月 13 2016 install.log.syslog
-rw-r--r--. 1 root root 46328 1月 13 2016 install.log
-rwxr-xr-x. 1 root root 1640731 6月 7 09:40 composer.phar
設置顯示方式
export TIME_STYLE='+%Y-%m-%d %H:%M:%S'
[root@localhost110 ~]# ls -l|sort -k 6
總用量 1740
-rw-r--r--. 1 root root 10033 2016-01-13 17:42:57 install.log.syslog
-rw-r--r--. 1 root root 46328 2016-01-13 17:48:20 install.log
-rw-------. 1 root root 1416 2016-01-13 17:48:28 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4096 2016-01-13 17:52:58 公共的
drwxr-xr-x. 2 root root 4096 2016-01-13 17:52:58 模板
drwxr-xr-x. 2 root root 4096 2016-01-13 17:52:58 視頻
drwxr-xr-x. 2 root root 4096 2016-01-13 17:52:58 圖片
drwxr-xr-x. 2 root root 4096 2016-01-13 17:52:58 文檔
drwxr-xr-x. 2 root root 4096 2016-01-13 17:52:58 下載
drwxr-xr-x. 2 root root 4096 2016-01-13 17:52:58 音樂
drwxr-xr-x. 2 root root 4096 2016-01-13 17:52:58 桌面
-rwxrwxrwx. 1 root root 272 2016-01-27 05:54:08 mysql_listen.sh
-rwxr-xr-x. 1 root root 1640731 2016-06-07 09:40:58 composer.phar
-rw-r--r--. 1 root root 42 2016-10-16 01:42:05 log
-rw-r--r--. 1 root root 8 2016-10-16 01:59:03 a.php
-rw-r--r--. 1 root root 143 2016-10-16 01:59:45 b.php
-rw-r--r--. 1 root root 140 2016-10-16 02:02:19 ab
-rw-r--r--. 1 root root 11 2016-10-16 02:06:07 A.php
-rw-r--r--. 1 root root 6 2016-10-16 02:17:17 1
-rw-r--r--. 1 root root 6 2016-10-16 02:17:21 B.php
-rw-r--r--. 1 root root 1112 2016-10-16 02:17:30 D.php
wc指令
wc [-lwm] filename 統計功能
-l 統計行
-w 統計詞
-m 統計字符
[root@localhost110 ~]# wc -l log
8 log
[root@localhost110 ~]# cat log|wc -l
8
[root@localhost110 ~]# cat log|wc -w
10
[root@localhost110 ~]# cat log -n
1 php
2 ajax
3 java
4 python
5 nginx mysql
6 GO p erlang
7 PHP5
8
[root@localhost110 ~]# cat log|wc -m
53
[root@localhost110 ~]# cat log1|wc -m
12
[root@localhost110 ~]# cat log1 -n
1 p h p
2 1 2 3

結尾算一個字符

還有 cut/join/paste等

Copyright © Linux教程網 All Rights Reserved