zoukankan      html  css  js  c++  java
  • shell脚本重定向和管道符

    1、重定向-tr
        程序 = 指令(命令) + 数据(变量)
        在程序中,数据如何输入和输出
       1) 数据输入:键盘 -- 标准输入,但是不是唯一输入方式
            echo "123456" | passwd --stdin "username"      //输入密码
            ./useradd.sh< user.txt        //把user.txt中文件重定向到useradd.sh中
       2) 数据输出:显示器--标准输出,但是不是惟一的输出方式。
            ls /etc/ > a.txt
            fd文件标识符0-9:相当于给文件分类
           0:标准输出
           1:标准输入
           2:错误输入输出
    常见重定向符号:
      1)标准输出
        >       覆盖重定向
        set -C  关闭覆盖重定向的功能   set +C:恢复
        >|    强制重定向    
        >>      追加重定向
      2)标准输入
        <     a<b   :把b交给a执行
        tr 替换文件内容(tr set1 [set2])char(字符:一个一个换)
        tr abc ABC < /etc/passwd :把etc/passwd中abc换成ABC
        << 将多行数据同时输入
      3)错误输出
        2>  2>>
        扩展:不需要输出内容,只需要输出状态;
        echo $?  :判断上一条命令是否正确(0正确,1-255其他都不正确)
    2、管道符-tee
        command1|command2|command3|...... :前一个命令的执行结果交给后一个命令来执行
        free -m | grep "^Mem"|cut -d ' ' -f19
        free -m | grep "^Mem"|awk'{print$3}'
        tee /tmp/tee.out //如果没有文件,会创建,默认如果文件存在有内容,会覆盖;
    练习:
    1)将/etc/passwd文件中的前5行内容转换为大写后保存至/tmp/passwd.out文件中
        head -5 /etc/passwd | tr [a-z] [A-Z] > /tmp/passwd.out
    2)将登录至当前系统上用户信息汇总的后3位信息转换为大写后保存至/tmp/who.out文件中
        who | tail -3 | cut -d ' ' -f1 | tr [a-z] [A-Z] tee /tmp/who.out

    tail 查看文件尾部多少行(10行)
         -n  tail -n /etc/passwd == 简写 tail -5 /etc/passwd
         -f  实时查看文件更新内容
          tail -f /var/log/meddage :实时查看日志文件
          head 查看文件头部多少行(10行)
          -n  #  前#行,
    例子: 取/etc/passwd文件中10-20行

    head -20 /etc/passwd | tail -10

  • 相关阅读:
    查找1
    动态规划
    分治
    [LeetCode] 1339. Maximum Product of Splitted Binary Tree
    [LeetCode] 1509. Minimum Difference Between Largest and Smallest Value in Three Moves
    [LeetCode] 233. Number of Digit One
    [LeetCode] 1963. Minimum Number of Swaps to Make the String Balanced
    [LeetCode] 1053. Previous Permutation With One Swap
    [LeetCode] 1962. Remove Stones to Minimize the Total
    [LeetCode] 1961. Check If String Is a Prefix of Array
  • 原文地址:https://www.cnblogs.com/hmm01031007/p/11347851.html
Copyright © 2011-2022 走看看