[20171225]变态的windows批处理4.txt
–//昨天学习windows 批处理的echo &.使用它可以实现类似回车换行的功能.例子:
1.echo &.
R:\>echo 1111 & echo 2222
1111
2222
–//但是如果写成如下:
R:\>echo 1111 & echo 2222 > aa.txt
1111
R:\>cat aa.txt
2222
–//你可以发现1111,显示输出,而2222写入文件aa.txt,改写成管道看看.
R:\>echo 1111 &echo 2222 | cat
1111
2222
–//OK.实际上这个是假象,第1行走屏幕,第2行走管道,看下面的测试就明白了.如果要写到文件实际上要加括号,这个跟linux有一个相似.
R:\>(echo 1111 &echo 2222 ) > aa.txt
R:\>cat aa.txt
1111
2222
–//这个倒是正常的情况.
2.利用这个特性可以通过管道传输命令给sqlplus.
R:\>echo set timing off head off; &echo select sysdate from dual;
set timing off head off;
select sysdate from dual;
R:\>echo set timing off head off; &echo select sysdate from dual; | sqlplus -s scott/book@78
set timing off head off;
SYSDATE
——————-
2017-12-25 10:06:33
–//晕!!明显set timing off head off;这行没有经过管道输出,而是直接输出到屏幕.因为如果输入管道,显示的应该是没有sysdate字段名.
–//仔细看前面的例子才发现实际上echo 1111 &echo 2222 | cat 输出1111走屏幕,而输出2222管道,看上去显示是正常的.
–//也就是要2行都通过管道必须使用括号.修改如下.
R:\>(echo set timing off head off; &echo select sysdate from dual; ) | sqlplus -s scott/book@78
2017-12-25 10:08:59
–//我google发现另外的写法,在&前加入^.
R:\>echo set timing off head off;^&echo select sysdate from dual; | sqlplus -s scott/book@78
2017-12-25 10:11:57
–//确实是Ok了,但是另外的问题来了:
R:\>echo set timing off head off;^&echo select sysdate from dual; | cat
set timing off head off;
select sysdate from dual;
R:\>echo set timing off head off;^&echo select sysdate from dual; > aa.txt
R:\>cat aa.txt
set timing off head off;&echo select sysdate from dual;
–//无法理解windows的批处理,通过管道输出2行.而使用文件接收显示的是set timing off head off;&echo select sysdate from dual;
–//重定向到文件时^实际上转义&.
set timing off head off; &echo select sysdate from dual;
–//而实际上这样执行是不行的.
R:\>cat aa.txt | sqlplus -s scott/book@78
Enter value for echo:
SP2-0546: User requested Interrupt or EOF detected.
–//还是不好理解windows的批处理的玄妙!!在我感觉最佳的方式还是加括号比较好理解一些.
–//实际上如果能很好理解链接http://blog.itpub.net/267265/viewspace-2140599/,通过管道实际上就是单行的批处理.如果能理解这个,上面的测试
–//就能很好理解.
–//但是如果echo里面有括号问题又来了:
R:\>(echo set timing off head off;&echo select (sysdate+1) from dual;) | sqlplus -s scott/book@78
此时不应有 from。
–//也就是)要转义,要转义3次.遇到这种情况不断增加^就是了.
R:\>(echo set timing off head off;&echo select (sysdate+1^^^) from dual;) | sqlplus -s scott/book@78
2017-12-26 11:16:33
–//而前面那种方式就简单了.
R:\>echo set timing off head off;^&echo select (sysdate+1) from dual; | sqlplus -s scott/book@78
2017-12-26 11:17:35
–//在我看来windows批处理真是变态加变态..