Batch - 重定向符号Redirection >
总结
Don't use a piping operator, which is what ">" Redirection is.
不要使用管道运算符,即“>”。
Display text 文本显示
To display a text on screen we have the ECHO command:
- ECHO Hello world
This will show the following text on screen:
- Hello world
When I say "on screen", I'm actually referring to the "DOS Prompt", "console" or "command window", or whatever other "alias" is used.
Streams 流
The output we see in this window may all look alike, but it can actually be the result of 3 different "streams" of text, 3 "processes" that each send their text to thee same window.
Those of you familiar with one of the Unix/Linux shells probably know what these streams are:
- Standard Output
- Standard Error
- Console
Standard Output is the stream where all, well, standard output of commands is being sent to.
The ECHO
command sends all its output to Standard Output.
ECHO 命令,都是将其输出到 Stand Output流中。
Standard Error is the stream where many (but not all) commands send their error messages.
And some, not many, commands send their output to the screen bypassing Standard Output and Standard Error, they use the Console. By definition Console isn't a stream.
There is another stream, Standard Input: many commands accept input at their Standard Input instead of directly from the keyboard.
Probably the most familiar example is MORE
:
- DIR /S | MORE
where the MORE
command accepts DIR
's Standard Output at its own Standard Input, chops the stream in blocks of 25 lines (or whatever screen size you may use) and sends it to its own Standard Output.
(Since MORE
's Standard Input is used by DIR
, MORE
must catch its keyboard presses (the "Any Key") directly from the keyboard buffer instead of from Standard Input.)
command >nul
^ This says to pipe the standard-output stream to null.
example:
- ECHO Hello world>NUL
will show nothing on screen. That's because >NUL
redirects all Standard Output to the NUL device, which does nothing but discard it.
command 2>nul
^ This says to pipe the standard-error stream to null.
command 2>&1
^ This says to pipe the standard-error stream to the same place as the standard-output stream.
Redirection 重定向
- uses 1 for Standard Output and 2 for Standard Error.
- 1>nul 意思是不显示命令运行的正确提示
- 2>nul 是不显示错误提示
如果放在一起就是, 正确提示&错误提示将都不显示
>是重定向符号, nul是空设备的意思, 把提示输入到空设备就不显示了
详细解释
You may be familiar with "redirection to NUL" to hide command output:
- ECHO Hello world>NUL
will show nothing on screen.
That's because >NUL
redirects all Standard Output to the NUL device, which does nothing but discard it.
Now try this (note the typo):
- EHCO Hello world>NUL
The result may differ for different operating system versions, but in Windows XP I get the following error message:
- 'EHCO' is not recognized as an internal or external command, operable program or batch file.
This is a fine demonstration of only Standard Output being redirected to the NUL device, but Standard Error still being displayed.
Redirecting Standard Error in "true" MS-DOS (COMMAND.COM) isn't possible (actually it is, by using the CTTY
command, but that would redirect all output including Console, and input, including keyboard).
In Windows NT 4 and later (CMD.EXE) and in OS/2 (also CMD.EXE) Standard Error can be redirected by using 2>
instead of >
A short demonstration. Try this command:
- ECHO Hello world 2>NUL
What you should get is:
- Hello world
You see? The same result you got with ECHO Hello world
without the redirection.
That's because we redirected the Standard Error stream to the NUL device, but the ECHO command sent its output to the Standard Output stream, which was not redirected.
Now make a typo again:
- EHCO Hello world 2>NUL
What did you get? Nothing
That's because the error message was sent to the Standard Error stream, which was in turn redirected to the NUL device by 2>NUL
When we use >
to redirect Standard Output, CMD.EXE interprets this as 1>
, as can be seen by writing and running this one-line batch file "test.bat":
DIR > NUL
Now run test.bat in CMD.EXE and watch the result:
- C:\>test.bat
- C:\>DIR 1>NUL
- C:\>_
It looks like CMD.EXE uses 1 for Standard Output and 2 for Standard Error. We'll see how we can use this later.
Ok, now that we get the idea of this concept of "streams", let's play with it.
Copy the following code into Notepad and save it as "test.bat":
- @ECHO OFF
- ECHO This text goes to Standard Output
- ECHO This text goes to Standard Error 1>&2
- ECHO This text goes to the Console>CON
Run test.bat in CMD.EXE, and this is what you'll get:
- C:\>test.bat
- This text goes to Standard Output
- This text goes to Standard Error
- This text goes to the Console
- C:\>_
Now let's see if we can separate the streams again.
Run:
- test.bat > NUL
and you should see:
- C:\>test.bat
- This text goes to Standard Error
- This text goes to the Console
- C:\>_
We redirected Standard Output to the NUL device, and what was left were Standard Error and Console.
Next, run:
- test.bat 2> NUL
and you should see:
- C:\>test.bat
- This text goes to Standard Output
- This text goes to the Console
- C:\>_
We redirected Standard Error to the NUL device, and what was left were Standard Output and Console.
Nothing new so far. But the next one is new:
- test.bat > NUL 2>&1
and you should see:
- C:\>test.bat
- This text goes to the Console
- C:\>_
This time we redirected both Standard Output and Standard Error to the NUL device, and what was left was only Console.
It is said Console cannot be redirected, and I believe that's true. I can assure you I did try!
CLS
).In this case, we could also have used test.bat >NUL 2>NUL
This redirects Standard Output to the NUL device and Standard Error to the same NUL device.
With the NUL device that's no problem, but when redirecting to a file one of the redirections will lock the file for the other redirection.
What 2>&1
does, is merge Standard Error into the Standard Output stream, so Standard output and Standard Error will continue as a single stream.
Batch - 重定向符号Redirection >的更多相关文章
- Batch: Display & Redirect Output
Batch How To ... Display & Redirect Output http://www.robvanderwoude.com/battech_redirection.php ...
- run commands in linux shell using batch file
adb shell as root after device rooted once device rooted, we must perform "su" before we g ...
- windows脚本-CMD和Batch
一.DOS,CMD和batch DOS是磁盘操作系统(英文:Disk Operating System)的缩写,是个人计算机上的一类操作系统.从1981年直到1995年的15年间,DOS在IBM PC ...
- batch.bat explaination
1.Echo 命令 打开回显或关闭请求回显功能,或显示消息.如果没有任何参数,echo 命令将显示当前回显设置. 语法 echo [{on|off}] [message] Sample篅echo of ...
- Web安全相关(三):开放重定向(Open Redirection)
简介 那些通过请求(如查询字符串和表单数据)指定重定向URL的Web程序可能会被篡改,而把用户重定向到外部的恶意URL.这种篡改就被称为开发重定向攻击. 场景分析 假设有一个正规网站http:// ...
- redis大幅性能提升之使用管道(PipeLine)和批量(Batch)操作
前段时间在做用户画像的时候,遇到了这样的一个问题,记录某一个商品的用户购买群,刚好这种需求就可以用到Redis中的Set,key作为productID,value 就是具体的customerid集合, ...
- Spring Batch在大型企业中的最佳实践
在大型企业中,由于业务复杂.数据量大.数据格式不同.数据交互格式繁杂,并非所有的操作都能通过交互界面进行处理.而有一些操作需要定期读取大批量的数据,然后进行一系列的后续处理.这样的过程就是" ...
- Cesium原理篇:Batch
通过之前的Material和Entity介绍,不知道你有没有发现,当我们需要添加一个rectangle时,有两种方式可供选择,我们可以直接添加到Scene的PrimitiveCollection,也可 ...
- spring batch资料收集
spring batch官网 Spring Batch在大型企业中的最佳实践 一篇文章全面解析大数据批处理框架Spring Batch Spring Batch系列总括
随机推荐
- Gym 102007I 二分 网络流
题意:给你一张图,每个城市有一些人,有不超过10个城市有避难所,避难所有容量上限,问最快多久可以让所有人进入避难所? 思路:二分时间,对于每个时间跑一遍最大流,判断最大流是不是人数即可.我们还需要用二 ...
- cmake 加入调试信息
1 首先在CMakeLists.txt中加入 SET(CMAKE_BUILD_TYPE "Debug")1在下面加入: SET(CMAKE_CXX_FLAGS_DEBUG &quo ...
- 基于React Native的跨三端应用架构实践
作者|陈子涵 编辑|覃云 “一次编写, 到处运行”(Write once, run anywhere ) 是很多前端团队孜孜以求的目标.实现这个目标,不但能以最快的速度,将应用推广到各个渠道,而且还能 ...
- sqlserver 将店铺表转换成可以用in查询的字符串
create TABLE #tempshopt ( shopid varchar(max) ) set @aSql = 'insert into #tempshopt(shopid) select s ...
- 【TCP】SYN攻击
TCP握手协议 在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接.第一次握手:建立连接时,客户端发送syn包(syn=j)到服务器,并进入SYN_SEND状态,等待服务器确 ...
- luoguP2590 [ZJOI2008]树的统计 [树链剖分] [TLE的LCT]
题目描述 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w. 我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. QMAX u ...
- A. Srdce and Triangle--“今日头条杯”首届湖北省大学程序设计竞赛(网络同步赛)
如下图这是“今日头条杯”首届湖北省大学程序设计竞赛的第一题,作为赛后补题 题目描述:链接点此 这套题的github地址(里面包含了数据,题解,现场排名):点此 Let be a regualr tr ...
- 十折交叉验证10-fold cross validation, 数据集划分 训练集 验证集 测试集
机器学习 数据挖掘 数据集划分 训练集 验证集 测试集 Q:如何将数据集划分为测试数据集和训练数据集? A:three ways: 1.像sklearn一样,提供一个将数据集切分成训练集和测试集的函数 ...
- Linux c基本知识整理
1.指针和引用的区别 1.指针是一个变量,变量存储一个地址指向内存中一个存储单元,需要单独分配内存空间.引用相当于变量的别名,不需要单独分配空间 2.引用必须初始化,指针可以先不进行初始化 3.指针可 ...
- JAVA中HashMap相关知识的总结(一)
Java中HashMap在jdk1.7和jdk1.8中的区别点: 在jdk1.7中是用数组+链表形式存储,1.8采用数组+链表/红黑树形式 Jdk1.8中由链表转为红黑树是长度大于8,由红黑树转为链表 ...