shell之路 Linux核心命令【第一篇】管道符与重定向
输出重定向
命令输出重定向的语法为:
command > file 或 command >> file
这样,输出到显示器的内容就可以被重定向到文件。果不希望文件内容被覆盖,可以使用 >> 追加到文件末尾
[root@hy ~]# who
root tty1 2015-09-03 16:21
root pts/3 2015-09-03 19:09 (192.168.11.1)
[root@hy ~]# who > output.txt
[root@hy ~]# cat output.txt
root tty1 2015-09-03 16:21
root pts/3 2015-09-03 19:09 (192.168.11.1)
输入重定向(<可省略)
command < file
[root@hy ~]# wc -l output.txt
2 output.txt
[root@hy ~]# wc -l < output.txt
[root@hy ~]# cat < output.txt
root tty1 2015-09-03 16:21
root pts/3 2015-09-03 19:09 (192.168.11.1)
[root@hy ~]# cat output.txt
root tty1 2015-09-03 16:21
root pts/3 2015-09-03 19:09 (192.168.11.1)
输入输出重定向深入
一般情况下,每个 Unix/Linux 命令运行时都会打开三个文件:
- 标准输入文件(stdin):stdin的文件描述符为0,Unix程序默认从stdin读取数据。
- 标准输出文件(stdout):stdout 的文件描述符为1,Unix程序默认向stdout输出数据。
- 标准错误文件(stderr):stderr的文件描述符为2,Unix程序会向stderr流中写入错误信息
一般情况下,只默认将stdout重定向到文件
[root@hy ~]# ls /root/ > output.txt
[root@hy ~]# cat output.txt
anaconda-ks.cfg
install.log
install.log.syslog
output.txt
use
stderr 重定向到 file
command 2 > file 或 command 2 >> file
[root@hy ~]# l /root/ > output.txt
-bash: l: command not found
[root@hy ~]# cat output.txt
[root@hy ~]# l /root/ 2> output.txt
[root@hy ~]# cat output.txt
-bash: l: command not found
stdout 和 stderr 合并后重定向到 file
command > file 2>&1 或 command >> file 2>&1
对 stdin 和 stdout 都重定向
[root@hy ~]# who > output.txt
[root@hy ~]# cat output.txt
root tty1 2015-09-03 16:21
root pts/3 2015-09-03 19:09 (192.168.11.1)
[root@hy ~]# cat output.txt > hy.txt
[root@hy ~]# cat < output.txt > hy1.txt
[root@hy ~]# cat hy.txt
root tty1 2015-09-03 16:21
root pts/3 2015-09-03 19:09 (192.168.11.1)
[root@hy ~]# cat hy1.txt
root tty1 2015-09-03 16:21
root pts/3 2015-09-03 19:09 (192.168.11.1)
Here Document
1.它的作用是将两个 delimiter 之间的内容(document) 作为输入传递给 command。
2.delimiter字符可自定义
语法:
command << delimiter
document
delimiter
实例
[root@hy ~]# wc -l << EOF
> root tty1 2015-09-03 16:21
> root pts/3 2015-09-03 19:09 (192.168.11.1)
> EOF
2
wc -l << EOF
root tty1 2015-09-03 16:21
root pts/3 2015-09-03 19:09 (192.168.11.1)
EOF
/dev/null文件
如果希望执行某个命令,但又不希望在屏幕上显示输出结果,那么可以将输出重定向到 /dev/null:
command > /dev/null
如果希望屏蔽 stdout 和 stderr,可以这样写:
command > /dev/null 2>&1
shell之路 Linux核心命令【第一篇】管道符与重定向的更多相关文章
- Linux核心命令
Linux核心命令 strace(查看系统调用的一个过程) 例:strace cat /test.txt netstat perf top pidstat mpstat dstat vmstat sl ...
- 《Linux大棚命令百篇下》网络篇的总结
本文是<Linux大棚命令百篇下>网络篇的总结 ping -c 指定数量,在windows下会自动停止,linux下会一直ping下去 -q 简短报告 -s 指定每次ping的数据包大小, ...
- Linux常用命令-解压缩篇
前言 Linux常用命令中,有很多用于对文件的压缩或解压,本文将介绍这些解压缩命令中不常见却非常实用的用法. tar tar是linux中最常用的解压缩命令.tar命令可用于处理后缀名为tar,tar ...
- LINUX常用命令 --- 权限篇
linux常用命令 linux用户权限相关 root 用户 相当于群主 超级用户 sudo命令 相当于群管理员 普通用户 群成员 查看用户id信息 使用linux ...
- 『学了就忘』Linux基础命令 — 33、管道符
目录 1.管道符介绍 2.管道符应用 (1)例子1: (2)例子2: (3)例子3: 1.管道符介绍 管道符|,也是Shell命令. 管道符的作用是链接多个命令,把命令1的结果作为命令2的操作对象. ...
- Linux apt命令使用 以及 文本流和重定向
apt (Advanced Packaging Tool) 是一个在Debian和Ubuntu中的Shell前端软件包管理器. apt命令执行需要超级管理员权限(root). apt语法 apt [o ...
- Linux命令第一篇
作业一: 1) 新建用户natasha,uid为1000,gid为555,备注信息为“master” natasha:x:1004:555:master:/home/natasha:/bin/ba ...
- Linux基础命令第一天
一.命令行bash基本操作 1,shell 用户不能直接操作内核,所以用户操作通过shell传递给内核,Linux下叫shell,就相当于Windows下的cmd shell分为两种: GUI:图形界 ...
- [linux time命令学习篇] time 统计命令执行的时间
注意: 命令后面一定要有分号; http://codingstandards.iteye.com/blog/798788 用途说明 time命令常用于测量一个命令的运行时间,注意不是用来显示和修改系统 ...
随机推荐
- node实现防盗链
什么是防盗链 盗链,顾名思义就是盗窃链接,就是在他人未经允许的情况下,去使用他人站点的任何图片,视频,内容等资源,来借此提升已用的站点的活跃度,严重损坏了别人的利益.那么防盗链就是采用一些办法阻止自己 ...
- Android MonkeyTalk测试
Android MonkeyTalk测试 MonkeyTalk可以用于压力测试,正因为这点所以才选择MonkeyTalk进行测试,相对于Monkey测试,目前个人发现的有点在于,MonkeyTalk是 ...
- 【Java】Junit单元测试
什么是单元测试? 单元测试(unit testing),是指对软件中的最小可测试单元进行检查和验证. 对于单元测试中单元的含义,一般来说,要根据实际情况去判定其具体含义,如C语言中单元指一个函数,Ja ...
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(九)之Interfaces
Interfaces and abstract classes provide more structured way to separate interface from implementatio ...
- 【three.js第七课】鼠标点击事件和键盘按键事件的使用
当我们使用鼠标操作three.js渲染出的对象时,不仅仅只是仅限用鼠标对场景的放大.缩小.旋转而已,还有鼠标左键.右键的点击以及键盘各种按键等等的事件.我们需要捕获这些事件,并在这些事件的方法里进行相 ...
- Python发送http请求时遇到问题总结
1.报错信息为“ERROR 'str' object has no attribute 'endwith'”,排查发现endswith方法名写错了,少了s,写成了 'endwith' if inter ...
- L26 使用卷积及循环神经网络进行文本分类
文本情感分类 文本分类是自然语言处理的一个常见任务,它把一段不定长的文本序列变换为文本的类别.本节关注它的一个子问题:使用文本情感分类来分析文本作者的情绪.这个问题也叫情感分析,并有着广泛的应用. 同 ...
- First Training
B B - Local Extrema CodeForces - 888A You are given an array a. Some element of this array ai is a l ...
- srt字幕翻译
需要把字幕名改成i.txt 有有道和谷歌 代码: #Author:Chenglong Qian #Copyright :Chenglong Qian import json import reques ...
- Say goodbye
Since September 28th 2015 Scriptogram officially closed. We considered every option before making th ...