Linux基础管道管理
一、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
Linux基础管道管理的更多相关文章
- Linux 基础——权限管理命令chmod
一.Linux中的文件权限与目录权限 Linux中定义了3种访问权限,分别是r.w.x.其中r表示对象是可读的,w表示对象是可写的,x表示对象是可执行的,这3种权限组成一组rwx分别对应对象的3个安全 ...
- Linux基础进程管理优先级
一.进程优先级 Linux进程调度及多任务 每个cpu(或者cpu核心)在一个时间点上只能处理一个进程,通过时间片技术,Linux实际能够运行的进程(和线程数)可以超出实际可用的cpu及核心数量.Li ...
- Linux基础用户管理
一.用户管理 (一).用户和组的基本概念 Users and groups:. Every process (running program) on the system runs as a part ...
- Linux基础-远程管理
shutdown 选项 时间 关机/重新启动 -r 重新启动 不指定选项和参数,1分钟后关闭电脑 重启必须加-r 示例: shutdown -r now now表示现在 shut ...
- linux基础权限管理
1.linux系统中的文件类型:- 一般文件 存放数据d 目录文件 存放其他文件l 链接文件 类似于windows系统中的快捷方式b 区块设备文件 可以随时读取c 字符设备文件 需要顺序读 ...
- Linux基础命令---管理组gpasswd
gpasswd gpasswd指令用来管理组文件“/etc/group”和“/etc/gshadow”,每个组可以设置管理员.组员.密码.系统管理员可以使用-A选项定义组管理员,使用-M选项定义成员. ...
- Linux 基础——权限管理命令chown、chgrp
一.chown命令与chgrp命令的作用 有时你需要改变文件或目录的属主,比如有人离职或开发人员创建了一个在测试或生产环境中需要归属在系统账户下的应用.Linux提供了两个命令来实现这个功能:chow ...
- Linux基础-12-yum管理软件包
1. yum的功能 yum是Yellow dog Updater, Modified的缩写,目的就是为了解决RPM的依赖关系的问题,方便使用者进行软件的安装.升级等等工作. 2. 光盘挂载和镜像挂载 ...
- Linux基础-11-rpm管理软件包
1. 使用RPM安装及移除软件 1) RPM的定义:RPM就是Red Hat Package Manger(红帽软件包管理工具)的缩写. 2) rpm的文件名分为5部分,其中: 第1部分是name,表 ...
随机推荐
- 【HDU - 2612】Find a way
-->Find a way 直接上Chinese Descriptions: hsj和lsh最近迷上了pokemon go的游戏.在双十一大物期中考试来临之前,他们想抓一只稀有土拨鼠来攒攒人品 ...
- docker命令总结
用 docker pull 拉取镜像 root@lishichao-virtual-machine:~# docker pull hello-world Using default tag: late ...
- Postman接口测试_基本功能
一. 安装与更新 1.安装的方式 方式1:chrome插件版本:chrome--->设置--->扩展程序: 方式2:native版本(具有更好的扩展性,推荐使用):https://ww ...
- filebeat直连elasticsearch利用pipeline提取message中的字段
这里使用filebeat直连elasticsearch的形式完成数据传输,由于没有logstash,所有对于原始数据的过滤略显尴尬(logstash的filter非常强大). 但是由于业务需求,还是需 ...
- 设计模式-抽象工厂模式(AbstractFactory)
抽象工厂模式是向客户端提供一个接口(FruitFactory),,使得客户端在不必指定产品的具体类型的情况下,能够创建多个产品族(NorthFruit.SouthFruit)的产品对象. 角色和职责: ...
- 关于AndroidStudio在编译时无法解析和拉取依赖的问题和无法访问Jcenter服务器的问题
问题描述:在编译时出现如下错误:Unknown host 'd29vzk4ow07wi7.cloudfront.net'. You may need to adjust the....一般是被墙了.偶 ...
- 使jira支持reopen率的统计
jira本身并不能统计bug的reopen率,虽然bug工作流程中有reopen节点,只能借助第三方插件来处理. 插件名称:Enhancer Plugin for JIRA,此插件支持自定义字段.自定 ...
- cookie、sessionSttorage、localStory区别
cookie.sessionSttorage.localStory都是在客户端以键值对存储的存储机制,并且只能将值存储为字符 cookie localStorage sessionStorage ...
- 【朝花夕拾】Android自定义View篇之(十一)View的滑动,弹性滑动与自定义PagerView
前言 由于手机屏幕尺寸有限,但是又经常需要在屏幕中显示大量的内容,这就使得必须有部分内容显示,部分内容隐藏.这就需要用一个Android中很重要的概念——滑动.滑动,顾名思义就是view从一个地方移动 ...
- LiteIDE TARGETARGS -conf_path=E:/PokerServer/src/GameServer/conf/texas.xml -log_dir=E:/PokerServer/src/GameServer/main/log
LiteIDE TARGETARGS -conf_path=E:/PokerServer/src/GameServer/conf/texas.xml -log_dir=E:/PokerServer/s ...