之前看到如下shell 命令,一头雾水:
ls temp >list.txt 2>&1
ls temp >/dev/null 2>&1
查阅之后,明白此语句含义,特此记录.
首先说明几点:
- 在unix和类unix(linux之类)中以文件描述符的形式打开一个文件,这个文件描述符是一个非负数。
- 0代表标准输入,1代表标准输出,2代表标准错误输出
- 默认的情况下是1 所以 > 相当于 1>
- shell命令的执行是从左往右
ls temp >list.txt 2>&1 (ps:temp是个不存在的目录)
ls temp >list.txt :
把标准输出重定向到list.txt文件中,在这里,就是把temp目录下所有的文件,列出来,然后输出到list.txt文件
如果没有这个重定向,那么标准输出默认是控制台
标准输出的句柄应该是1 ,所以,完整的写法应该是:ls temp 1>list.txt
2>&1 :
在此处 ,表示把标准错误输出写入到list.txt文件
经过这个重定向,标准输出和标准错误输出都重定向到了list.txt中
结果如下:
root:ls temp >list.txt 2>&1
root:/opt/tmp # more list.txt
ls: cannot access temp: No such file or directory
//error 信息在文件里
ls temp >/dev/null 2>&1
这个表示把标准输出,以及标准错误输出都舍弃了,不显示也不保存
如果换一下顺序,如何?
ls temp 2>&1 >list.txt
标准错误输出定向到控制台,标准内容输出定向到list.txt
结果如下:
root:/opt/tmp # ls temp 2>&1 >out.txt
ls: cannot access temp: No such file or directory
//error信息直接输出到控制台
总结:
ls temp :表示标准内容输出和标准错误都输出到控制台
等同于:
ls temp>&1
ls temp >list :表示标准内容输出到list文件中,标准错误输出还是在控制台
等同于:
ls temp 1>list
ls temp >list 2>&1 :表示标准内容输出到list文件中,标准错误输出也在list文件中
等同于:
ls temp >list 2>list
ls temp 1>list 2>list
ls temp 1>list 2>&1
ls temp 2>& >list :标准错误输出也在控制台,表示标准内容输出到list文件中
等同于:
ls temp 2>& 1>list 参考文献:
http://www.cnblogs.com/caolisong/archive/2007/04/25/726896.html
http://www.ningoo.net/html/2007/shell_scripts_stderr_stdout.html
分享到:
相关推荐
Linux的shell scripting 是一种自动完成多种类型任务的方法,从夜间备份到简单的命令行应用。几乎任何程序都可以通过shell script完成。你甚至可以在脚本内部完成简单的条件检查。简单的Shell scripting对于处理...
this program imitates the Linux Shell Commands is include all the BASH commands (ls, echo, gedit, rm,write to file, etc) to run the commands just type them as usual ##用法: 1. open Terminal ...
我构建了hadoop集群。我们一定会写一个shell脚本去每一个节点上去jps,查看每个节点的进程情况。 原先以为shell很简单: #!/bin/bash #查看每个节点运行情况 for((host=101;...-bash: jps: command not found
在Linux Shell中有很多的特殊符号,这对于我们写Shell脚本时要特别留意:一方面要知道这些特殊符号的用法,这些符号用好了可以达到事半功倍的效果;但另一方面要避免这些特殊符号的过度使用而导致脚本难以调试、难以...
NULL 博文链接:https://ggsonic.iteye.com/blog/1537374
运维常用linux命令_shell技巧范例
The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by...
Linux Shell Scripting Cookbook is a collection of essential command-line recipes along with detailed descriptions tuned with practical applications. It covers most of the commands on Linux with a ...
所有linux命令的源代码 cat cp date df dd ls .....
Shell是一种具备特殊功能的程序, 它是介于使用者和 UNIX/Linux 操作系统之核心程序(kernel)间的一个接口。 为了对用户屏蔽内核的复杂性,也为了保护内核以免用户误操作造成损害,在内核的周围建了一个外壳(shell)...
Linux Shell Scripting Cookbook is a collection of essential command-line recipes along with detailed descriptions tuned with practical applications. It covers most of the commands on Linux with a ...
The shell environment should contain shell=<pathname>/myshell where <pathname>/myshell is the full path for the shell executable (not a hardwired path back to your directory, but the one from which it...
shell. Then, you'll learn how to write a simple bash script and how to edit your bash script using Linux editors. Following this, you will learn how to define a variable and the visibility of a ...
Linux的shell好比DOS的COMMAND.COM,在用户和系统之间架起一座桥梁,让用户方便地操作系统。由于Linux是一个完全开放的平 台,有许多人都为它编写了Shell程序。其中的BASH(Baurne Again SHell)容纳了其他Shell程序的...
Linux Shell Scripting Cookbook is a collection of essential command-line recipes along with detailed descriptions tuned with practical applications. It covers most of the commands on Linux with a ...
Shell scripting is a quick method to prototype a complex application or a problem by automating tasks when working on Linux-based systems. Using both simple one-line commands and command sequences ...
Chapter 2: Performing Essential Command Line tasks Chapter 3: Administering the Linux File System Chapter 4: Working with Text Files Chapter 5: Managing Partitions and Logical Volumes Chapter 6: ...
The Bash shell is a complete programming language, not merely a glue to combine external Linux commands. By taking full advantage of Shell internals, Shell programs can perform as snappily as ...
Linux_command_line, Linux命令和 shell 脚本简介 LinuxLinux命令和 shell 脚本简介为更完整的资源列表,包括初学者的教程, Linux Linux curated资源列表有关资源的更多信息,请访问脚本教程。 Linux介绍插件什么是...