• Linux管道及I/O重定向


    I/O:

    系统设定

      默认输入设备:标准输入,STDIN,0

      默认输出设备:标准输出,STDOUT,1

      标准错误输出:STDERR,2

      属于不同的数据流

    标准输入:键盘

    标准输出和错误输出:显示器

    I/O重定向:

    输出重定向:

    > :覆盖输出

    >> :追加输出

    2>:错误输出

    2>>:追加错误输出

    正常输出

    ls /usr > /tmp/var.out

    set

      -C:禁止对已经存在文件使用覆盖重定向;

      +C:允许覆盖输出

    强制覆盖输出

    ls /usr >| /tmp/var.out

    错误输出

    ls /varr 2> /tmp/var2.out

    定向标准输出与标准错误输出

    ls /varr > /tmp/var3.out 2> /tmp/err.out

    &>:重定向标准输出或错误输出至同一个文件

    ls /varr &> /tmp/var4.out 

    输入重定向

    <:正常输入

    <<:Here Document

    cat << EOF

    正常输入

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

    输出内容到文件中

    cat >> /tmp/myfile.txt << EOF

    管道:前一个命令的输出,作为后一个命令的输入

    命令1 | 命令2 | 命令3 ...

    echo "First love is only a little foolishness and a lot of curiosity." | tr 'a-z' 'A-Z'

    echo "redhat" | passwd --stdin hive

    cat /etc/passwd | sort 

    cut -d : -f 1 /etc/passwd | sort -n | tr 'a-z' 'A-Z'

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

    wc -l /etc/passwd | cut -d' ' -f 1

    tee :从标准输入读取,写入到标准输出,并保存到文件中

    echo "First love is only a little foolishness and a lot of curiosity." | tee  /tmp/test.out

    练习:

    1.统计/usr/bin目录下的文件个数;

     #ls /usr/bin | wc -l

    2.取出当前系统上所有用户的shell,每种shell只显示一次,且按顺序显示;

    # /etc/passwd 保存shell

    #cut -d : -f 7 /etc/passwd |sort -u

    3.如何显示/var/log目录下每个文件的内容类型?

    #file /var/log/*

    4.取出/etc/inittab文件的第6行;

    # head -6 /etc/inittab | tail -1

    5.取出/etc/passwd文件中倒数第9个用户的用户名和shell,显示到屏幕上并将其保存至/tmp/users文件中;

    # tail -9 /etc/passwd | head -1 | cut -d : -f 1,7 | tee /tmp/users

    6.显示/etc目录下所有以pa开头的文件,并统计其个数;

    #  ls -d /etc/pa* | wc -l

    7.不使用文本编辑器,将alias cls=clear一行内容添加至当前用户的.bashrc文件中。

    # echo "alias cls=clear" >> ~/.bashrc

  • 相关阅读:
    高斯消元学习
    HDU 4596 Yet another end of the world(解一阶不定方程)
    Codeforces Round #318 div2
    HDU 4463 Outlets(一条边固定的最小生成树)
    HDU 4458 Shoot the Airplane(计算几何 判断点是否在n边形内)
    HDU 4112 Break the Chocolate(简单的数学推导)
    HDU 4111 Alice and Bob (博弈)
    POJ 2481 Cows(线段树单点更新)
    HDU 4288 Coder(STL水过)
    zoj 2563 Long Dominoes
  • 原文地址:https://www.cnblogs.com/isunny/p/5870361.html
Copyright © 2020-2023  润新知