LinuxDay5——标准I/O和管道

一、三种I/O设备

标准输入和输出

程序:指令 +数据

  读取数据:Input

  输出数据:Output

打开的文件都有一个fd:file description(文件描述符)

Linux给程序提供三种I/O设备

  标准输入(STDIN)-0 默认接受来自键盘的输入

  标准输出(StDOUT) -1 默认给输出到终端窗口

  标准错误)(STDRR)-2  默认输出到终端窗口

I/O重定向:改变默认位置

把输出和错误重定向到文件

STDOUT和STDERR可以被重定向到文件

  命令  操作符号  文件名

支持的操作符号包括

  >  把STDOUT重定向到文件

  2>  把STDERR重定向到文件

  &>  把所有输出重定向到文件

>文件内容会被覆盖

  set   -C禁止将内容覆盖已有文件,但可追加

  > | file  强制覆盖

  set  +C允许覆盖

>>原有内容基础上,追加内容

2>覆盖重定向错误输出数据流

2>>追加重定向错误输出数据流

标准输出和错误输出各自定向至不同位置

  COMMAND > /path/to/file.out 2> /path/to/error.out

合并标准输出和错误输出为同一个数据流进行重定向

  &>覆盖重定向

  &>>追加重定向

  COMMAND > /path/to/file.out 2>&1(顺序很重要)

  COMMAND >>/path/to/file.out 2>&1

():合并多个程序的STDOUT

  (cal 2007;cal 2008)> all.txt 

1 [root@centos6 ~]#ls /error /data > log1 2>&1
2 [root@centos6 ~]#ls /error /data 2>&1 > log2
3 ls: cannot access /error: No such file or directory
4 [root@centos6 ~]#ls /error /data &> log3
5 [root@centos6 ~]#ls /error /data 2>log4 >&2
重定向
[root@centos6 ~]#cat f1
#bin/bash
#i want to print something
echo "hello word"
[root@centos6 ~]#cat <f1 >f1
[root@centos6 ~]#cat f1
覆盖

 tr命令

  tr转换和删除字符

  tr[OPTION]...SET1 [SET2]

  选项

  -c -C——complement:  去字符集的补集

  -d——delete:      删除所有属于第一字符集的字符

  -s——squeezu  -  repeats: 把连续重复的字符以单独一个字符表示

  -t——truncate - set1:   将第一个字符集对应字符转化为第二字符集对应的字符

  [:alnum:]:字母和数字   [:alpha:]:字母     [:cntrl:]:控制(非打印)字符 [:digit:]:数字
  [:graph:]:图形字符    [:lower:]:小写字母   [:print:]:可打印字符 [:punct:]:标点符号
  [:space:]:空白字符    [:upper:]:大写字母   [:xdigit:]:十六进制字符

二、把I/O重定向至文件

使用<来重定向标准输入

某些命令能够接受从文件中导入的STDIN

  tr 'a-z' 'A-Z' < /etc/issue

  该命令会把/etc/issue中的小写字符都转换成大写字符

tr -d abc < /etc/fstab 删除fstab文件中所有abc中任意字符

cat  > file

 mage

 wangxiaochun

 按Ctrl + d 离开,可以使用文件来代替键盘输入

 Cat > filea <fileb

把多行发送给STDIN

使用“<<终止词”命令从键盘把多行重导向给STDIN

 直到 终止词 位置的所有文本都发送给STDIN

 有事被称为就地文本(heretext)

三、使用管道

管道(使用符号“|”表示)用来连接命令

  COMMAND1 | COMMAND2 | COMMAND3

  将命令1的STDOUT发送给命令2的STDIN,命令2的STDIN发送到命令3的STDIN

  STDERR默认不能通过管道转发,可利用2>&1或 |&实现

  最后一个命令会在当前shell进程的字shell进程中执行用来组合多种工具的功能

  ls | tr 'a-z' 'A-Z'

less:一页一页地查看输入

  ls -l  /etc | less

mail:通过电子邮件发送输入

  echo "test email" | mail -s "test' user@example.com

ipr:把输入发送给打印机

  echo "test print" | lpr -P printer_name

管道中 “-”符号

示例:

  将、home里的文件打包,但打包带数据不是记录到文件,而是传送到stdout,经过管道后,将tar -cvf - /home 传送给后面的 tar -xvf -,后面这个-则是取前一个命令的stdout,因此,就不需要使用临时的file了

[root@centos6 ~]#tar -cvf - /home | tar -xvf -
tar: Removing leading `/' from member names
/home/
/home/gentoo/
/home/gentoo/.gnome2/
/home/gentoo/.bashrc
/home/gentoo/.mozilla/
/home/gentoo/.mozilla/plugins/
/home/gentoo/.mozilla/extensions/
/home/gentoo/.bash_logout
/home/gentoo/.bash_profile
/home/varnish/
/home/varnish/.gnome2/
/home/varnish/.bashrc
/home/varnish/.mozilla/
/home/varnish/.mozilla/plugins/
/home/varnish/.mozilla/extensions/
/home/varnish/.bash_logout
home/
/home/varnish/.bash_profile
/home/nginx/
/home/nginx/.gnome2/
/home/nginx/.bashrc
/home/nginx/.mozilla/
/home/nginx/.mozilla/plugins/
/home/nginx/.mozilla/extensions/
/home/nginx/.bash_logout
/home/nginx/.bash_profile
home/gentoo/
home/gentoo/.gnome2/
home/gentoo/.bashrc
home/gentoo/.mozilla/
home/gentoo/.mozilla/plugins/
home/gentoo/.mozilla/extensions/
home/gentoo/.bash_logout
home/gentoo/.bash_profile
home/varnish/
home/varnish/.gnome2/
home/varnish/.bashrc
home/varnish/.mozilla/
home/varnish/.mozilla/plugins/
home/varnish/.mozilla/extensions/
home/varnish/.bash_logout
home/varnish/.bash_profile
home/nginx/
home/nginx/.gnome2/
home/nginx/.bashrc
home/nginx/.mozilla/
home/nginx/.mozilla/plugins/
home/nginx/.mozilla/extensions/
home/nginx/.bash_logout
home/nginx/.bash_profile
管道 - 符号

重定向到多个目标(tree

命令1 | tee [-a] 文件名 | 命令2

  把命令1的STDOUT保存在文件中,作为命令2的输入

  -a 追加

使用

  保存不同阶段的输出

  复杂管道的故障排除

  同时查看和记录输出

四、练习

1、将/etc/issue文件中的内容转换为大写文件后保存至/tem/issue.out文件中

[root@centos6 /]#mkdir tem
[root@centos6 /]#tr 'a-z' 'A-Z' < /etc/issue >/tem/issue.out
[root@centos6 /tem]#cat issue.out
CENTOS RELEASE 6.9 (FINAL)
KERNEL \R ON AN \M
text1

2、将当前系统登录用户的信息转换为大写后保存至/tmp/who.out文件中

[root@centos6 ~]#who | tr 'a-z' 'A-Z' >/tmp/who.out
[root@centos6 ~]#cat /tmp/who.out
ROOT     TTY1         2018-03-30 08:01 (:0)
ROOT     PTS/0        2018-03-30 08:01 (192.168.190.1
text2

3、一个linux用户给root发邮件,要求邮件标题为”help”,邮件正文如下:
Hello, I am 用户名,The system version is here,please help me to check it ,thanks!
操作系统版本信息

[root@centos6 ~]#mail -s "help" root <<end
> hello,I am $USER,
> The system version is here,please hellp me to check it,thanks
> OS version:`lsb_release -a`
> end
[root@centos6 ~]#mail
Heirloom Mail version 12.4 7/29/08.  Type ? for help.
"/var/spool/mail/root": 5 messages 1 new 3 unread
 U  1 Anacron               Sat Mar 24 09:13  26/1249  
root                  Thu Mar 29 09:57  20/695   
root                  Fri Mar 30 08:11  20/696   
 U  4 Mail Delivery System  Fri Mar 30 08:11  74/2466  
>N  5 root                  Fri Mar 30 08:36  24/919   
& 5
Message  5:
From root@centos6.localdomain  Fri Mar 30 08:36:25 201
Return-Path: <root@centos6.localdomain>
X-Original-To: root
Delivered-To: root@centos6.localdomain
Date: Fri, 30 Mar 2018 08:36:25 +0800
To: root@centos6.localdomain
Subject: help
User-Agent: Heirloom mailx 12.4 7/29/08
Content-Type: text/plain; charset=us-ascii
From: root@centos6.localdomain (root)
Status: R

hello,I am root,
The system version is here,please hellp me to check it
,thanks
OS version:LSB Version:    :base-4.0-amd64:base-4.0-noarc
h:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:gr
aphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noar
ch
Distributor ID:    CentOS
Description:    CentOS release 6.9 (Final)
Release:    6.9
Codename:    Final
text3

4、将/root/下文件列表,显示成一行,并文件名之间用空格隔开

[root@centos6 ~]#ls | tr '\n' ' '
anaconda-ks.cfg Desktop Documents Downloads f1 home install.log install.log.syslog log1 log2 log3 log4 motd Music Pictures Public rm Templates Videos 
text4

5、计算1+2+3+..+99+100的总和

[root@centos6 ~]#echo {1..100} | tr ' ' '+' |bc
5050


[root@centos6 ~]#seq -s + 1 100 | bc
5050
text5

6、删除Windows文本文件中的‘^M’字符

[root@centos6 ~]#tr -d '\r' < win.txt 
a
b
c
text6

7、处理字符串“xt.,l 1 jr#!$mn 2 c*/fe 3 uz 4”,只保留其中的数字和空格

[root@centos6 ~]#echo "xt.,l 1 jr#win.txtmn 2 c*/fe 3 uz 4" | tr -d '[:alpha:]''[:punct:]'
 1  2  3  4
text7

8、将PATH变量每个目录显示在独立的一行

[root@centos6 ~]#echo $PATH | tr ':' '\n'
/usr/lib64/qt-3.3/bin
/usr/local/sbin
/usr/local/bin
/sbin
/bin
/usr/sbin
/usr/bin
/root/bin
[root@centos6 ~]#
text8

9、将指定文件中0-9分别替代成a-j

[root@centos6 ~]#echo {0..20} | tr '0-9' 'a-j'
a b c d e f g h i j ba bb bc bd be bf bg bh bi bj ca
text9

10、将文件/etc/centos-release中每个单词(由字母组成)显示在独立的一行,并无空行

[root@centos6 ~]#cat /etc/centos-release | tr ' ' '\n'CentOS
release
6.9
(Final)
text10
posted @ 2018-04-03 10:38  Gmiao  阅读(289)  评论(0编辑  收藏  举报