歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> I/O重定向和管道

I/O重定向和管道

日期:2017/3/3 11:03:01   编辑:Linux技術

一.I/O重定向

1.標准輸入和輸出

程序=指令+數據

數據又分為:input(讀入數據)+output(輸出數據)

linux給程序提供三種I/O設備

標准輸入(stdin)-0 默認接受來自鍵盤的輸入

標准輸出(stdout)-1 默認輸出到終端窗口

標准錯誤(stderr)-2,默認輸出到終端窗口

2.輸出重定向

(1)覆蓋從定向 :>

command >/path/to/file

實例:a重定向覆蓋a1

[root@localhost testdir]# cat a1
[root@localhost testdir]# cat a
hello world!
[root@localhost testdir]# cat a >a1
[root@localhost testdir]# cat a1
hello world! (2)追加重定向:>>

command >>/path/to/file

實例:把etc/issue內容追加到a1中

[root@localhost testdir]# cat /etc/issue >>a1

[root@localhost testdir]# cat a1

hello world!

CentOS release 6.8 (Final)

Kernel \r on an \m

(3)錯誤輸出:2>或2>>

command 2>/path/to/file 覆蓋輸出

command 2>>/path/to/file追加輸出

(4)標准輸出和錯誤輸出

command &>/path/to/file

command &>>/path/to/file

或command >/path/to/file 2>&1

command >>/path/to/file 2>>&1

實例:

[root@localhost testdir]# cat /etc/issue /etc/abc &>f1
[root@localhost testdir]# cat f1
CentOS release 6.8 (Final)
Kernel \r on an \m                        #正確輸出信息

cat: /etc/abc: No such file or directory  #錯誤輸出信息
上述實例也可以這樣寫
[root@localhost testdir]# cat /etc/issue /etc/abc >f2 2>&1
[root@localhost testdir]# cat f2
CentOS release 6.8 (Final)
Kernel \r on an \m

cat: /etc/abc: No such file or directory
(5)用cat直接將輸入的信息輸出到catfile中,當鍵盤輸入eof時輸入結束,利用<<eof,我們可以終止一次輸入

實例:

[root@localhost testdir]# cat <catfile <<eof
> hello world
> how are you
> eof
-bash: catfile: No such file or directory
實例:向zhang用戶寫一封求助信
[root@localhost testdir]# mail -s "help" zhang <<eof
> hello,I am `id -nu` #查看用戶
> the system version is here,please check it.
> `cat /etc/issue` #查看內核版本
> eof
(6)tr 轉換和刪除字符

tr[-ds] SET1

-d:刪除信息當中的SET1這個字符串

-s:替換掉重復的字符

實例:

將last輸出的信息中所有的小寫字符變成大寫

[root@centos7 ~]# last | tr [a-z] [A-Z]
ROOT     PTS/1        10.1.250.64      SAT JUL 30 12:54   STILL LOGGED IN   
ZHANG    PTS/2        10.1.250.64      SAT JUL 30 12:54   STILL LOGGED IN   
ZHANG    PTS/1        :0               SAT JUL 30 12:50 - 12:54  (00:03)    
ZHANG    PTS/0        :0               SAT JUL 30 12:50   STILL LOGGED IN 
......
將/etc/passwd中的冒號刪除
[root@localhost testdir]# cat /etc/passwd | tr -d ':'
rootx00root/root/bin/bash
binx11bin/bin/sbin/nologin
daemonx22daemon/sbin/sbin/nologin
admx34adm/var/adm/sbin/nologin
......
將/root/下文件列表,顯示成一行,並文件名之間用空格隔開。

[root@centos7 /]# ls -a /root | tr '\n' ' '
. .. anaconda-ks.cfg .bash_history .bash_logout .bash_profile .bashrc .cache cat .config .cshrc .dbus -h history.log initial-setup-ks.cfg mm .tcshrc
二.管道整體管道命令如下所示:

command1 | command2 | command3

注意;(1)每個管道後面接的第一個數據必須是"命令",例如:less,more,head,tail等

(2)管道命令必須會處理standard output,對於standard error output予以忽略

(3)管道命令必須能接收來自前一個命令的數據成為standard input繼續處理才行

下面來介紹一些命令:

1.cut

cut -d'分隔字符' -f files

cut -c 字符范圍

選項:

-d:後面跟分隔字符,與-f一起使用

-f:依據-d的分隔字符將一段信息切割成數段,用-f取出第幾段

-c:以字符的單位取出固定字符區間

實例:

將PATH變量取出,顯示第3個路徑

[root@centos7 /]# echo $PATH | cut -d: -f 3
/usr/sbin2.重定向到多個目標

tee 屏幕顯示並繼續進行管道處理

command1 |tee file | commmand2

實例:

當前登錄用戶顯示到屏幕並轉換為大寫

[root@centos7 /]# who |tee f4 | tr 'a-z' 'A-Z'
ZHANG    :0           2016-07-30 12:50 (:0)
ZHANG    PTS/0        2016-07-30 12:50 (:0)
ZHANG    PTS/2        2016-07-30 12:54 (10.1.250.64)
ROOT     PTS/1        2016-07-30 16:04 (10.1.250.64)
本文出自 “zhang1003995416” 博客,請務必保留此出處http://1003995416.blog.51cto.com/10482168/1832136

Copyright © Linux教程網 All Rights Reserved