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命令常用于测量一个命令的运行时间,注意不是用来显示和修改系统 ...
随机推荐
- Python设计模式(7)-建造者模式
# coding=utf-8 class Report: def create_head(self): pass def create_body(self): pass class year_Repo ...
- std::string::copy函数
size_t copy (char* s, size_t len, size_t pos = 0) const;
- posix系统线程调度-设置线程优先级
#include <thread> #include <mutex> #include <iostream> #include <chrono> #in ...
- java 代码执行cmd 返回值异常 (关于JAVA Project.waitfor()返回值是1)
关于JAVA Project.waitfor()返回值是1 0条评论 Project.waitfor()返回值是1,找了很久从网上没有发现关于1的说明. 这时对源代码调试了一下,发现Project ...
- 钩子函数 Function类
Function 为 com.google.common.base包下接口类: public interface Function<F, T> { @Nullable T apply(@N ...
- 数据结构和算法(Golang实现)(18)排序算法-前言
排序算法 人类的发展中,我们学会了计数,比如知道小明今天打猎的兔子的数量是多少.另外一方面,我们也需要判断,今天哪个人打猎打得多,我们需要比较. 所以,排序这个很自然的需求就出来了.比如小明打了5只兔 ...
- 【python实现卷积神经网络】开始训练
代码来源:https://github.com/eriklindernoren/ML-From-Scratch 卷积神经网络中卷积层Conv2D(带stride.padding)的具体实现:https ...
- D - Three Integers CodeForces - 1311D
题意: a<=b<=c 输出A,B,C要求B是A的倍数,C是B的倍数,并且输出a,b,c变成A,B,C需要的最小次数. 题解:写了半天的二分,后来发现思路错了,,,暴力就能过.. 三层fo ...
- 【认证与授权】Spring Security的授权流程
上一篇我们简单的分析了一下认证流程,通过程序的启动加载了各类的配置信息.接下来我们一起来看一下授权流程,争取完成和前面简单的web基于sessin的认证方式一致.由于在授权过程中,我们预先会给用于设置 ...
- 0day学习笔记(3)--修改函数返回地址
环境: devc++(编译改为32位),windows10 源码(来自书中) #include <stdio.h> #define PASSWORD "1234567" ...