zoukankan      html  css  js  c++  java
  • 6. Linux输入输出重定向

    1.输入重定向是指把文件导入到命令中,而输出重定向则是指把原本要输出到屏幕的数据信息写入到指定文件中。

      输入重定向中用到的符号及其作用

       

      输出重定向中用到的符号及其作用

      

      1)通过输出重定向将原本要输出到屏幕的信息写入到文件中。  

    [root@Centos test]# man bash > readme.txt
    [root@Centos test]# ll
    total 304
    -rw-r--r--. 1 root root    122 Aug  4 16:58 a.txt
    -rw-r--r--. 1 root root 303223 Aug  5 15:45 readme.txt
    [root@Centos test]# head -n 5 readme.txt 
    BASH(1)                                   General Commands Manual                                  BASH(1)
    
    
    
    NAME
    

      2)重定向的覆盖写入和追加写入  

    [root@Centos test]# echo "Hello I'm Xinghen1216" > readme.txt 
    [root@Centos test]# cat readme.txt 
    Hello I'm Xinghen1216
    
    [root@Centos test]# echo "I'm glad to meet you" >> readme.txt 
    [root@Centos test]# cat readme.txt 
    Hello I'm Xinghen1216
    I'm glad to meet you

      3)把命令的报错信息写入到文件(常用于执行自动化的shell脚本中)。  

    [root@Centos test]# ll abc.txt
    ls: cannot access abc.txt: No such file or directory
    [root@Centos test]# 
    [root@Centos test]# ll abc.txt 2>error.txt
    [root@Centos test]# ll
    total 12
    -rw-r--r--. 1 root root 122 Aug  4 16:58 a.txt
    -rw-r--r--. 1 root root  53 Aug  5 16:00 error.txt
    -rw-r--r--. 1 root root  43 Aug  5 15:50 readme.txt
    [root@Centos test]# cat error.txt 
    ls: cannot access abc.txt: No such file or directory

    2.管道命令符  

      把前一个命令原本要输出到屏幕的数据当作是后一个命令的标准输入。

      举例:1)通过匹配关键词/sbin/nologin 找出了所有被限制登录系统的用户  

    [root@Centos test]# grep "/sbin/nologin" /etc/passwd | wc -l
    35

      2)用翻页的形式查看/etc目录中的文件列表及属性信息  

    [root@Centos test]# ll /etc/ | more
    total 1348
    drwxr-xr-x.  3 root root      101 Jul 30 01:27 abrt
    -rw-r--r--.  1 root root       16 Jul 30 01:36 adjtime
    -rw-r--r--.  1 root root     1518 Jun  7  2013 aliases
    -rw-r--r--.  1 root root    12288 Jul 30 01:38 aliases.db
    drwxr-xr-x.  2 root root       51 Jul 30 01:28 alsa

      3)解决自动化脚本中遇到的修改密码需要输入两次密码确认的问题

      举例:不需确认,将root密码直接修改成"centos"

    [root@Centos test]# echo "centos" | passwd --stdin root
    Changing password for user root.
    passwd: all authentication tokens updated successfully.

      4)计算1+2+...+100   

    [root@Centos test]# echo {1..100}
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    [root@Centos test]# 
    [root@Centos test]# echo {1..100} | tr ' ' '+'
    1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+32+33+34+35+36+37+38+39+40+41+42+43+44+45+46+47+48+49+50+51+52+53+54+55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70+71+72+73+74+75+76+77+78+79+80+81+82+83+84+85+86+87+88+89+90+91+92+93+94+95+96+97+98+99+100
    [root@Centos test]# 
    [root@Centos test]# 
    [root@Centos test]# echo {1..100} | tr ' ' '+' | bc
    5050
  • 相关阅读:
    Wireshark安装、简单使用、过滤器简介
    iOS中多线程原理与runloop介绍
    iphone客户端上传图片到服务器
    微信公众平台用户分组管理开发文档详解
    微信公众平台开发:进阶篇(Web App开发入门)
    如何使用NSOperations和NSOperationQueues
    IOS 多线程的一些总结
    Fiddler (五) Mac下使用Fiddler
    在其他app里预览文档
    UIWebView加载ANSI格式的txt文件出现乱码问题解决
  • 原文地址:https://www.cnblogs.com/xinghen1216/p/13441285.html
Copyright © 2011-2022 走看看