shell之重定向
使用>和>>都表示向结果重定向到一个文件中,区别在于>是覆盖式的重定向,会先将内容先清空,然后再将结果输入,而>>是追加式的重定向,是将要输入的内容追加在在已存在的内容后面,并不会清空文件。
实例:
[root@localhost ~]# echo 123456 > a.txt
[root@localhost ~]# cat a.txt
123456
[root@localhost ~]# echo 78910 >> a.txt
[root@localhost ~]# cat a.txt
123456
78910
[root@localhost ~]# echo 2468 > a.txt
[root@localhost ~]# cat a.txt
2468
重定向符号>之前的数字(0表示标准输入,1表示标准输出,2表示错误输出),如果>之前没有添加数字,则默认为1,表示将 要正常显示的内容重定向到指定文件,如果出现错误,错误信息将显示在屏幕,而不会重定向到文件中,则不会将错误也写入文件中;如果 > 之前写的是2,表示如果出现错误,则将错误信息重定向到文件,而正常的命令的结果内容仍旧正常显示。
比如下面的例子:
ubuntu@ubuntu:~$ echo hello 1> a.txt
ubuntu@ubuntu:~$ cat a.txt
hello
ubuntu@ubuntu:~$ #等价与下面这个语句
ubuntu@ubuntu:~$ echo hello > a.txt
ubuntu@ubuntu:~$ cat a.txt
hello
ubuntu@ubuntu:~$ #当发生错误时,2> 会将错误的信息输出到文件中
ubuntu@ubuntu:~$ #而正常部分的内容仍会正常显示
ubuntu@ubuntu:~$ show 2> a.txt;echo world;
world
ubuntu@ubuntu:~$ #上一条命令中的show出现错误了,但是并没有出现错误信息,因为错误信息重定向到了a.txt中
ubuntu@ubuntu:~$ #第二条命令正常运行,所以结果正常显示。
The program 'show' can be found in the following packages:
* mailutils-mh
* nmh
单独使用>或者2>,只能将错误或者正确的运行结果重定向到指定文件中,而如果要让正常运行的结果和出现异常时的提示信息都重定向到文件中的话,可以使用&>,注意没有&>>这种语法,即不能追加,但是可以通过其他方法实现。如下例:
#!/bin/bash
#test.sh ls
catt /
执行脚本test.sh
ubuntu@ubuntu:~$ bash test.sh
a.txt Documents examples.desktop Pictures Templates Videos
Desktop Downloads Music Public test.sh
test.sh: line 5: catt: command not found
ubuntu@ubuntu:~$ cat a.txt
a.txt Documents examples.desktop Pictures Templates Videos
Desktop Downloads Music Public test.sh
ubuntu@ubuntu:~$ bash test.sh &>a.txt
ubuntu@ubuntu:~$ cat a.txt
a.txt Documents examples.desktop Pictures Templates Videos
Desktop Downloads Music Public test.sh
test.sh: line 5: catt: command not found
尝试将输出(包含正确命令的输出和错误命令的提示信息)以追加方式重定向到一个文件中,重点在2>&1 表示将错误输出(2)也重定向到标准输出(1)中的管道中。
ubuntu@ubuntu:~$ bash test.sh 1>a.txt 2>&1 a.txt #覆盖
ubuntu@ubuntu:~$ cat a.txt
a.txt Desktop Documents Downloads examples.desktop
Music Pictures Public Templates test.sh Videos
test.sh: line 5: catt: command not found
ubuntu@ubuntu:~$ bash test.sh 1>a.txt 2>&1 a.txt #覆盖
ubuntu@ubuntu:~$ cat a.txt
a.txt Desktop Documents Downloads examples.desktop
Music Pictures Public Templates test.sh Videos
test.sh: line 5: catt: command not found
ubuntu@ubuntu:~$ bash test.sh 1>>a.txt 2>&1 a.txt #追加方式
ubuntu@ubuntu:~$ cat a.txt
a.txt Desktop Documents Downloads examples.desktop
Music Pictures Public Templates test.sh Videos
test.sh: line 5: catt: command not found
a.txt Desktop Documents Downloads examples.desktop
Music Pictures Public Templates test.sh Videos
test.sh: line 5: catt: command not found
ubuntu@ubuntu:~$
shell之重定向的更多相关文章
- Linux Shell系列教程之(十六) Shell输入输出重定向
本文是Linux Shell系列教程的第(十六)篇,更多Linux Shell教程请看:Linux Shell系列教程 Shell中的输出和输入的重定向是在使用中经常用到的一个功能,非常实用,今天就为 ...
- shell 数据流重定向操作符总结
最近看了鸟哥私房菜关于shell数据流重定向的内容,总结一下. 操作符: 1.标准输入(stdin):代码为0,符号:< 或者<< 2.标准输出(stdout):代码为1,符号:&g ...
- [转]linux shell数据重定向(输入重定向与输出重定向)详细分析
在了解重定向之前,我们先来看看linux 的文件描述符. linux文件描述符:可以理解为linux跟踪打开文件,而分配的一个数字,这个数字有点类似c语言操作文件时候的句柄,通过句柄就可以实现文件 ...
- Linux——模拟实现一个简单的shell(带重定向)
进程的相关知识是操作系统一个重要的模块.在理解进程概念同时,还需了解如何控制进程.对于进程控制,通常分成1.进程创建 (fork函数) 2.进程等待(wait系列) 3.进程替换(exec系列) 4 ...
- Shell中重定向<<EOF注意事项
作者:iamlaosong 我们常常在shell脚本程序中用<<EOF重定向输入.将我们输入的命令字符串作为一个运行程序的输入,这样,我们就不须要在那个程序环境中手工输入命令,以便自己主动 ...
- linux shell数据重定向
标准输入 (stdin) :代码为 0 ,使用 < 或 << :标准输出 (stdout):代码为 1 ,使用 > 或 >> :标准错误输出(stderr):代码为 ...
- shell日志重定向到null
用输出重定向符号> 即可,格式如下:shell命令 >/dev/null 若要将标准错误输出也一并重定向,如下:shell命令 >/dev/null 2>&1这样就不管 ...
- 【Shell脚本学习24】Shell输入输出重定向:Shell Here Document,/dev/null文件
Unix 命令默认从标准输入设备(stdin)获取输入,将结果输出到标准输出设备(stdout)显示.一般情况下,标准输入设备就是键盘,标准输出设备就是终端,即显示器. 输出重定向 命令的输出不仅可以 ...
- Linux shell之重定向输入,输出
shell是一个命令解释器,它在操作系统的最外层,负责直接与用户对话,把用户的输入解释给操作系统,并处理各种各样的操作系统的输出结果,输出到屏幕返回给用户.这种对话方式可以是交互的方式(从键盘输入命令 ...
- shell 输入输出重定向
1. 命令列表: command > file 将输出重定向到file command < file 将输入重定向到file command >> file 将输出以追加的方式 ...
随机推荐
- 控件_SeekBar与RatingBar
这两种进度条都是ProgressBar的子类 SeekBar:是一种可以拖动的进度条,比如播放音乐的进度 import android.app.Activity; import android.os. ...
- Find a way
Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year ...
- Django 使用mysql 数据库流程
创建一个mysql数据库 在settings中配置: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': ...
- python第四十六课——函数重写
3.函数重写(override) 前提:必须有继承性 原因: 父类中的功能(函数),子类需要用,但是父类中函数的函数体内容和我现在要执行的逻辑还不相符 那么可以将函数名保留(功能还是此功能),但是将函 ...
- (15)Python时间
- 编译apache报APR not found
系统环境: [vagrant@rs-2 download]$ cat /etc/redhat-release CentOS release 5.6 (Final) [vagrant@rs-2 dow ...
- Codeforces Round #553 (Div. 2) D. Stas and the Queue at the Buffet 贪心+公式转化
题意 给出n个pair (a,b) 把它放在线性序列上 1--n 上 使得 sum(a*(j-1)+b*(n-j)) 最小 思路 :对式子进行合并 同类项 有: j*(a-b)+ (-a+ ...
- ethereum/EIPs-1
https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1.md 介绍了什么是EIP等等的详细信息: eip title status type a ...
- ConcurrentHashMap中的putIfAbsent方法的使用以及返回值的含义
public V putIfAbsent(@NotNull K key, @NotNull V value) putIfAbsent方法主要是在向ConcurrentHashMap中添加键—值对的时候 ...
- Python2.7-内置函数
具体参见:https://docs.python.org/2/library/functions.html#file 1.进制转换:bin(x), oct(x), hex(x) 把一个十进制数分别转换 ...