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

6. Linux输入输出重定向的更多相关文章

  1. 全面理解Linux输入输出重定向

    全面理解Linux输入输出重定向 本教程通过视频方式讲解shell操作,理解感念,教程通俗易懂,比起看一大堆文档要舒服的多.本次教程主要讲解  Linux Shell 中支持输入输出重定向,用符号&l ...

  2. Linux输入输出重定向和文件查找值grep命令

    Linux输入输出重定向和文件查找值grep命令 一.文件描述符Linux 的shell命令,可以通过文件描述符来引用一些文件,通常使用到的文件描述符为0,1,2.Linux系统实际上有12个文件描述 ...

  3. [转帖]Linux教程(12)- linux输入输出重定向

    Linux教程(12)- linux输入输出重定向 2018-08-21 22:57:02 钱婷婷 阅读数 49更多 分类专栏: Linux教程与操作 Linux教程与使用   版权声明:本文为博主原 ...

  4. Linux 输入输出重定向, &>file, 2>&1, 1>&2

    Linux 输入输出重定向, &>file, 2>&1, 1>&2 一.1和2在Linux中代表什么 1.1 输出重定向 1.2 输入重定向 1.3 绑定重定 ...

  5. linux 输入输出重定向

    输入输出重定向 1,输入输出重定向,是针对过滤器的,不针对,编辑器和交互工具 2,>号只把正确的标准输出重定向,输出错误信息,可以用2> 3,新建或清空文件可以直接用>filenam ...

  6. linux输入输出重定向

    http://www.cnblogs.com/chengmo/archive/2010/10/20/1855805.html 在Linux下,当一个用户进程被创建的时候,系统会自动为该进程创建三个数据 ...

  7. [100]linux输入输出重定向

    一目了然版本: &号含义: 参考 参考:非常经典,值得一看,我是在linux爱好者公众号里发现的. 下面是我自己的一些总结. linux的命令数据流 在Linux下,当一个用户进程被创建的时候 ...

  8. linux - 输入输出重定向 及 管道

    > 正确结果重定向 2> 错误结果重定向 &> 正确和错误全部重定向 >> 追加,其它同> 标准输出实际上就是显示器,比如我们使用cat命令打开一个文件,文 ...

  9. Linux输入输出重定向练习

    1.date >> 123 date > 123 2.abc 2>123 abc 2>>123 abc 2>/dev/null  标准输出重定向到回收站 3. ...

随机推荐

  1. Java学习_泛型

    什么是泛型. Java标准库提供的ArrayList内部就是一个Object[]数组,配合存储一个当前分配的长度,就可以充当"可变数组". public class ArrayLi ...

  2. Go GRPC 入门(一)

    前言 微服务相关 使用 GRPC 通讯的 Golang 微服务入门 举例写一个微服务,接收网址发送请求获取返回结果返回 正文 安装工具 安装 protobuf 这是 proto 文件的编译器 点我下载 ...

  3. nginx文件结构与解析,例子

    1.nginx文件结构 1 ... #全局块 2 3 events { #events块 4 ... 5 } 6 7 http #http块 8 { 9 ... #http全局块 10 server ...

  4. Docker 介绍和安装(一)

    # 下载阿里云的 Centos7 的docker.repo # step 1: 安装必要的一些系统工具 sudo yum install -y yum-utils device-mapper-pers ...

  5. 【Spring】Spring的数据库开发 - 2、Spring JdbcTemplate的常用方法(execute、update、query)

    Spring JdbcTemplate的常用方法 文章目录 Spring JdbcTemplate的常用方法 execute() update() query() 简单记录-Java EE企业级应用开 ...

  6. 【Docker】安装docker18.09.6后,无法启动

    ------------------------------------------------------------------------------------------------- | ...

  7. k8s用kubectl管理应用升级,服务发布与回滚,扩缩容

    应用升级 Kubectl set image --help 有案例指定新版本 [root@k8s-master ~]# kubectl set image deployment/nginx nginx ...

  8. 你这样用过DO循环吗?

    DATA: BEGIN OF text,        word1(4) TYPE c VALUE 'This',        word2(4) TYPE c VALUE 'is',         ...

  9. 算法模板 - C++ 高精度运算

    C++算法板子 高精度 高精度推荐用python来写,python有大整数,这里写的是关于C++的高精度运算模板 1.高精 * 低精 #include <iostream> #includ ...

  10. JAVA之路_假克隆、浅克隆、深克隆

    一.JAVA假克隆 Java中,对于基本类型,可以用"="进行克隆,而对于引用类型却不能简单的使用"="进行克隆,这与JAVA的内存使用空间有关,JAVA在栈中 ...