zoukankan      html  css  js  c++  java
  • 运维基础-IO 管道

    什么是文件描述符FD或者文件句柄?

    通过构建一个带有编号标记的通道(文件描述符)的进程结构来管理打开的文件。今晨连接到文件,从而达到这些文件所代表的的数据内容或者设备。通过使用通道0、1、2(称为标准输入,标准输出,标准错误)的默认连接创建进程。进程使用3号及以上标号的通道连接其他文件。

    掌握常见文件描述符的作用

    0 表示stdin(标准输入),默认连接为键盘,仅读取
    
    1 表示stdout(标准输出),默认连接为终端,仅写入
    
    2 表示stderr(标准错误),默认连接为终端,仅写入
    
    3+ 表示filename(其他文件),可以读取/写入

    示例:

    保存时间戳

    [root@localhost ~]# date
    2018年 08月 24日 星期五 09:03:31 CST
    [root@localhost ~]# date > /tmp/saved-timestamp
    [root@localhost ~]# cat /tmp/saved-timestamp 
    2018年 08月 24日 星期五 09:03:46 CST
    [root@localhost ~]# 

    将一个文件的最后100行保存到另一个文件

    [root@localhost ~]# tail -n 100 /var/log/demsg > /tmp/last-100-boot-messages
    [root@localhost ~]# 

    将四个文件合并为一个

    [root@localhost ~]# cat file1 file2 file3 file4 > /tmp/all-four-in-one
    [root@localhost ~]# 

    累出家目录中的隐藏文件名和常规文件名保存到文件中

    [root@localhost ~]# ls -a > /tmp/my-file-names
    [root@localhost ~]# cat /tmp/my-file-names 
    .
    ..
    anaconda-ks.cfg
    .bash_history
    .bash_logout
    .bash_profile
    .bashrc
    .cache
    .config
    .cshrc
    .dbus
    Desktop
    .gitconfig
    initial-setup-ks.cfg
    .mozilla
    .pki
    .ssh
    .tcshrc
    .viminfo
    .xauthKYrxVH
    .Xauthority
    [root@localhost ~]# 

    使用普通用户对系统目录进行访问会被拒绝,将错误从定向到文件

    [root@localhost ~]# find /etc -name passwd 2 > /tmp/errors
    [root@localhost ~]# 

    将命令的输出和错误消息分别保存到单独的文件中

    [root@localhost ~]# find /etc/-name passwd > /tmp/output 2 > /tmp/errors
    [root@localhost ~]#

    忽略并丢弃错误消息

    [root@localhost ~]# find /etc/ -name passwd > /tmp/output 2> /dev/null
    [root@localhost ~]#

    基于输出及输入重定向实现管理

    head /tail/wc/cut/sort/uniq/diff/patch/tr/grep

    进程管道PIping

    示例:

    将ls长葛市输出分页显示

    [root@localhost ~]# ls -l /usr/bin/ | less
    
    
    
    
    
    总用量 173472
    -rwxr-xr-x.   1 root root      41496 11月  6 2016 [
    -rwxr-xr-x.   1 root root     107856 8月   3 2017 a2p
    -rwxr-xr-x.   1 root root      11248 8月  10 2017 abrt-action-analyze-backtrace
    -rwxr-xr-x.   1 root root      15328 8月  10 2017 abrt-action-analyze-c
    -rwxr-xr-x.   1 root root       1345 8月  10 2017 abrt-action-analyze-ccpp-local
    -rwxr-xr-x.   1 root root       6821 8月  10 2017 abrt-action-analyze-core
    -rwxr-xr-x.   1 root root      11224 8月  10 2017 abrt-action-analyze-oops
    -rwxr-xr-x.   1 root root      11232 8月  10 2017 abrt-action-analyze-python

    计算ls输出的行数并且保存到文件

    [root@localhost ~]# ls | wc -l > /tmp/how-many-files
    [root@localhost ~]# cat /tmp/how-many-files 
    3
    [root@localhost ~]# 

    将ls输出的前10行保存到文件

    [root@localhost ~]# ls -t | head -n 10 > /tmp/ten-last-changed-files
    [root@localhost ~]# cat /tmp/ten-last-changed-files 
    Desktop
    initial-setup-ks.cfg
    anaconda-ks.cfg
    [root@localhost ~]# 
    只有三行。。。

    在终端商显示ls列表,同时将文件列表存储到文件中

    [root@localhost ~]# ls -l | tee /tmp/saved-output
    总用量 8
    -rw-------. 1 root root 1635 8月  20 19:18 anaconda-ks.cfg
    drwxr-xr-x. 2 root root    6 8月  23 18:14 Desktop
    -rw-r--r--. 1 root root 1666 8月  20 19:21 initial-setup-ks.cfg
    [root@localhost ~]# cat /tmp/saved-output 
    总用量 8
    -rw-------. 1 root root 1635 8月  20 19:18 anaconda-ks.cfg
    drwxr-xr-x. 2 root root    6 8月  23 18:14 Desktop
    -rw-r--r--. 1 root root 1666 8月  20 19:21 initial-setup-ks.cfg
    [root@localhost ~]# 

     确定当前窗口的终端设备,将ls结果作为邮件发送,并在此窗口查看输出内容

    [root@localhost ~]# ls -l | tee /dev/pts/1 | mail -s subject seven_nighter@163.com
    总用量 8
    -rw-------. 1 root root 1635 8月  20 19:18 anaconda-ks.cfg
    drwxr-xr-x. 2 root root    6 8月  23 18:14 Desktop
    -rw-r--r--. 1 root root 1666 8月  20 19:21 initial-setup-ks.cfg
    您在 /var/spool/mail/root 中有邮件
    [root@localhost ~]# 

    掌握参数传递机制Xargs

    xargs命令是给其他命令传递参数的一个过滤器,也是组合多个命令的一个工具,它擅长将标准输入数据转换成命令行参数,xargs能够处理管道或者stdin并且将其转换成特定的命令的参数。xargs也可以将单行或者多行文本输入转换为其他格式,例如多行变单行,单行变多行。xargs的默认命令时echo,空格是默认的定界符,这就意味着通过管道传递给xargs的输入将会包含换行和空白,不过通过xargs的处理,换行和空白将被空格取代,xargs是构建单行命令的重要组件之一。

     具体操作百度。。

     wc - print newline, word,and bytes counts for each file

    计算文件的行数,单词数,字节数

    [root@localhost ~]# wc /etc/passwd
      42   82 2146 /etc/passwd
    [root@localhost ~]# 
    [root@localhost ~]# wc -l /etc/passwd
    42 /etc/passwd                 行数
    [root@localhost ~]# 
    [root@localhost ~]# wc -w /etc/passwd
    82 /etc/passwd                 单词数
    [root@localhost ~]# 
    [root@localhost ~]# wc -c /etc/passwd
    2146 /etc/passwd               字节数
    [root@localhost ~]# 

    按列提取文件

    -d 指明列分隔符 默认tab

    -f 选择输出的区域

    -c 指定字符位置

    [root@localhost ~]# cut -d: -f1 /etc/passwd
    root
    bin
    daemon
    adm
    lp
    sync
    shutdown
    halt
    mail
    operator
    [root@localhost ~]# cut -c1-3 /etc/passwd
    roo
    bin
    dae
    adm
    lp:
    syn
    shu

    sort - sort lines of text files

    排序输出,默认按照首字符从头至尾的顺序排序

    -r 逆序(倒叙)

    -n 按数字排序

    -t 指明分隔符 与 -k 连用

    -k 按指定的域排序

    [root@localhost ~]# sort -t: -k3 /etc/passwd | cut -d: -f3 | head -n 5
    0
    1000
    1001
    107
    11
    [root@localhost ~]# 
    [root@localhost ~]# sort -t: -k3 -n /etc/passwd | cut -d: -f3 | head -n 5
    0
    1
    2
    3
    4
    [root@localhost ~]# 
    [root@localhost ~]# sort -t: -k3 -n -r /etc/passwd | cut -d: -f3 | head -n 5
    65534
    1001
    1000
    999
    998
    [root@localhost ~]# 

    。。。。更加纤细的请自行百度。

  • 相关阅读:
    luogu 1169 棋盘制作(单调栈/悬线)
    poj 2769 感觉♂良好 (单调栈)
    hdu 5033 buiding(单调栈)
    hdu1506 直方图中最大的矩形 单调栈入门
    有线电视网(树形dp)
    洛谷P1220 关路灯(区间dp)
    【题解】NOI2009二叉查找树 + NOIP2003加分二叉树
    【题解】AHOI2009中国象棋
    【算法】Matrix
    【题解】WC2008游览计划
  • 原文地址:https://www.cnblogs.com/52-qq/p/9528100.html
Copyright © 2011-2022 走看看