一、I/O重定向

标准输入,标准输出,标准错误

file descriptors (FD, 文件描述符或Process I/O channels);

进程使用文件描述符来管理打开的文件

[root@linux ~]# ls /proc/$$/fd
        

, , and , known as standard input, standard output, and standard error

输出重定向(覆盖,追加)

正确输出:1>  1>>  等价于  >  >>

错误输出:2>  2>>

案例1:输出重定向(覆盖)

[root@linux ~]# date > date.txt


案例2:输出重定向(追加)

[root@linux ~]# date >> date.txt 


案例3:错误输出重定向

[root@linux ~]# ls /home/  /aaaaaaaaa >list.txt
ls: 无法访问/aaaaaaaaa: 没有那个文件或目录
[root@linux ~]# ls /home/ /aaaaaaaaa >list.txt >error.txt //重定向到不同的位置


案例4:正确和错误都输入到相同位置

[root@linux ~]# ls /home/  /aaaaaaaaa &>list.txt                        //混合输出


案例5:正确和错误都输入到相同位置

[root@linux ~]# ls /home/  /aaaaaaaaa >list.txt >&                //重定向到相同的位置


案例6:重定向到空设备/dev/null

[root@linux ~]# ls /home/  /aaaaaaaaa >list.txt >/dev/null         //空设备,即将产生的输出丢掉
[root@linux ~]# ls /home/ /aaaaaaaaa &>/dev/null //空设备,即将产生的输出丢掉


案例7:脚本中使用重定向

[root@linux ~]# vim ping1.sh
ping -c1 10.18.40.100
if [ $? -eq ];then
echo "10.18.40.100 is up."
else
echo "10.18.40.100 is down!"
fi
[root@linux ~]# vim ping1.sh
[root@linux ~]# chmod +x ping1.sh
[root@linux ~]# ./ping1.sh
[root@linux ~]# vim ping1.sh
ping -c1 10.18.40.100 &>/dev/null
if [ $? -eq ];then
echo "10.18.40.100 is up."
else
echo "10.18.40.100 is down!"
fi

案例8:脚本中使用重定向

[root@linux ~]# vim ping2.sh
ping -c1 10.18.40.100 &>/dev/null
if [ $? -eq ];then
echo "10.18.40.100 is up." >>up.txt
else
echo "10.18.40.100 is down!" >>down.txt
fi
[root@linux ~]# vim ping2.sh
[root@linux ~]# chmod +x ping1.sh
[root@linux ~]# ./ping2.sh

二、输入重定向

标准输入:<  等价  0<

案例1:

[root@linux ~]# mail alice                                    //没有改变输入的方向,默认键盘
Subject: hello .
EOT
[root@linux ~]# su - alice
[alice@alice ~]$ mail
Heirloom Mail version 12.5 //. Type ? for help.
"/var/spool/mail/alice": message new
>N root Mon Jul : / "hello"
[root@linux ~]# mail -s "test01" alice < /etc/hosts    //输入重定向,来自于文件

案例2:

[root@linux ~]# grep 'root'                                    //没有改变输入的方向,默认键盘,此时等待输入...
yang sss
sssrootssss..
sssrootssss..
[root@linux ~]# grep 'root' < /etc/passwd
root:x:::root:/root:/bin/bash
operator:x:::operator:/root:/sbin/nologin

案例3:

[root@linux ~]# dd if=/dev/zero of=/file1.txt bs=1M count=
[root@linux ~]# dd </dev/zero >/file2.txt bs=1M count=

案例4:mysql表结构导入

[root@linux ~]# mysql -uroot -p123 < bbs.sql

案例5:at

[root@linux ~]# at now + min
at> useradd yang99
at> <EOT>
job at Mon Jul ::
[root@linux ~]# vim at.txt
sudo useradd yang100
sudo useradd yang102
[root@liwei ~]# at now + min <a.txt
job at Mon Jul ::

三、综合案例

案例1:利用重定向建立多行的文件(手动执行shell命令)

[root@linux ~]# echo "" > file1.txt
[root@linux ~]# cat file1.txt [root@linux ~]# cat >file2.txt ^D
[root@linux ~]# cat file2.txt

案例2:利用重定向建立多行的文件 脚本script创建多行文件

[root@linux ~]# vim create_file.sh
cat >file200.txt <<EOF yyy
ccc
EOF
[root@linux ~]# bash create_file.sh [root@linux ~]# cat file200.txt yyy
ccc

案例3: 脚本中利用重定向打印消息

[root@linux ~]# cat create_file.sh
cat <<-EOF yyy
ccc
EOF
[root@linux ~]# bash create_file.sh yyy
ccc

[root@liwei ~]# vim yang.sh
cat <<-EOF
+------------------------------------------------+
| |
| ====================== |
| 虚拟机基本管理 v4. |
| by sky_king |
| ====================== |
| . 安装KVM |
| . 安装或重置CentOS-6.8 |
| . 安装或重置CentOS-7.3 |
| . 安装或重置RHEL-6.4 |
| . 安装或重置Windows- |
| . 删除所有虚拟机 |
| q. 退出管理程序 |
| |
+------------------------------------------------+
EOF

案例4

[root@linux ~]# ls; date &>/dev/null                                      //希望两条命令输出都重定向 ??

[root@linux ~]# ls &>/dev/null; date &>/dev/null

[root@linux ~]# (ls; date) &>/dev/null

[root@linux ~]# (while :; do date; sleep ; done) &               //在后台运行,但输出依然在前台终端
[]
[root@linux ~]# 2017年 08月 01日 星期二 :: CST
2017年 08月 01日 星期二 :: CST
2017年 08月 01日 星期二 :: CST
2017年 08月 01日 星期二 :: CST
2017年 08月 01日 星期二 :: CST [root@linux ~]# (while :; do date; sleep ; done) &>date.txt &
[root@linux ~]# tailf /date.txt
2017年 08月 01日 星期二 :: CST
2017年 08月 01日 星期二 :: CST
2017年 08月 01日 星期二 :: CST
2017年 08月 01日 星期二 :: CST
2017年 08月 01日 星期二 :: CST
2017年 08月 01日 星期二 :: CST
2017年 08月 01日 星期二 :: CST

最新文章

  1. OpenGL(一)——入门学习
  2. 关于javascript
  3. 原来你是个这样的JVM
  4. linux下getrusage()
  5. Java ---Listener监听器
  6. RDMA调研报告&amp;一点随笔
  7. Python内置函数(55)——globals
  8. Nginx 学习笔记(九)申请Let&#39;s Encrypt通配符HTTPS证书
  9. 通过Nginx反向代理之后客户端验证码session不一致造成无法验证通过的问题解决
  10. python三大框架之一flask应用
  11. 关于vue搭建项目运行出行的错误问题,简直是大坑啊
  12. 初学者在Mysql8.0连接时的几个常见基本问题
  13. VMware Lab setup - A virtualized lab for testing HA and DRS
  14. dotNet core 应用部署centos
  15. 51、多线程创建的三种方式之实现Callable接口
  16. [5] 柱台(Cylinder)图形的生成算法
  17. bzoj 4443: [Scoi2015]小凸玩矩阵
  18. elasticsearch不能使用root启动问题解决
  19. 网络传输层之TCP/UDP详解
  20. VMWARE里安装时出现&#39;SMBus Host Controller not enabled&#39;

热门文章

  1. ES6_07_Symbol属性
  2. Python 的8个关键要素
  3. Flags Over Objects
  4. [Python学习]错误篇二:切换当前工作目录时出错——FileNotFoundError: [WinError 3] 系统找不到指定的路径
  5. 嵊州D4T2 硬币 有人来教教我吗!
  6. 让Redis突破内存大小的限制
  7. Excel中vlookup函数使用
  8. 一篇文章学会Docker命令
  9. PG利用Multicorn访问CSV外部数据源
  10. 内核下载、nfs挂载:个性问题及解决方法~~共勉