中国DOS联盟

-- 联合DOS 推动DOS 发展DOS --

联盟域名:www.cn-dos.net 论坛域名:www.cn-dos.net/forum
DOS,代表着自由开放与发展,我们努力起来,学习FreeDOS和Linux的自由开放与GNU精神,共同创造和发展美好的自由与GNU GPL世界吧!

中国DOS联盟论坛
现在时间是 2026-08-08 19:01
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » 用实例说明管道命令,组合命令的用法(方便新手) 查看 745 回复 7
楼 主 用实例说明管道命令,组合命令的用法(方便新手) 发表于 2008-05-22 17:07 ·  中国 山东 淄博 联通
银牌会员
★★★
积分 1,604
发帖 646
注册 2008-04-13 23:39
18年会员
UID 115804
性别 男
状态 离线
高手们无视吧。。。- -||

@echo off
title PORT T0 PROCESS
:1
cls&echo.&set "tmp="
set "port="
set/p "port=Input the port id: "
if not defined port goto 1
echo %port%|findstr /v /i "">nul&&call :lp||goto 1

:lp
for /f "skip=4 tokens=3,7 delims=: " %%a in ('netstat -anop tcp') do (
echo %%a|findstr "\<%port%\>">nul&&(set tmp=A&echo.&echo PROCESS INFOMATION:&echo.&tasklist /fi "pid eq %%b" 2>nul)
)
if not defined tmp echo.&echo No %port% port running
pause>nul&goto 1



@ 
useage:
Hide the command which is behind it.

&
usage:
No matter the first command succeed or failed, execute the second command.

&&
usage:
While first command succeed ,then second command.

||
usage:
While first command failed,then second command.

|
usage:
Communicate the result of first command to the parameters of the second command.

>
usage:
Output the result of the command into a file and cover the original contents.

>>
usage:
Output the result of the command into a file,but will not cover the original contents.



熟用管道命令, && || 可以代替 if errorlevel 感觉简单多了啊~
心绪平和,眼藏静谧,无比安稳的火... Purification of soul...Just a false...^_^
2 发表于 2008-05-22 17:31 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
积分 3,105
发帖 1,276
注册 2008-03-08 13:00
18年会员
UID 112398
性别 男
状态 离线
兄弟举的例子是不是太难懂了点啊,我都看了半天没明白过来。

[ Last edited by zw19750516 on 2008-5-22 at 05:33 PM ]
批处理之家新域名:www.bathome.net
3 发表于 2008-05-22 17:35 ·  中国 山东 淄博 联通
银牌会员
★★★
积分 1,604
发帖 646
注册 2008-04-13 23:39
18年会员
UID 115804
性别 男
状态 离线
就是输入一个端口号,输出所对应的进程。。。。 另外加了一点排错处理。。 昨晚上没事做,消遣一下的~
心绪平和,眼藏静谧,无比安稳的火... Purification of soul...Just a false...^_^
4 发表于 2008-05-22 17:38 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
积分 3,105
发帖 1,276
注册 2008-03-08 13:00
18年会员
UID 112398
性别 男
状态 离线
学批处理的新手怕是难以理解啊,兄弟。
ps:好像不对啊,我的机器上196端口是开着的,但运行后却提示no run!

[ Last edited by zw19750516 on 2008-5-22 at 05:46 PM ]
批处理之家新域名:www.bathome.net
5 发表于 2008-05-22 17:46 ·  中国 山东 淄博 联通
银牌会员
★★★
积分 1,604
发帖 646
注册 2008-04-13 23:39
18年会员
UID 115804
性别 男
状态 离线
最主要的是常练习。。。 以前写过这样一个批处理,端口--进程。。。以前的代码比现在繁杂3倍之多~ 只要是还不懂for的流程~ 一心钻研下去,总会有收获的~ 我就是个例子~ 另外学了这好几个月的批处理,if errorlevel 的用法始终摸不到头脑,所以就提醒下新手可以&& ||代替If errorlevel
一下就罗嗦了这么多~ 无视吧
心绪平和,眼藏静谧,无比安稳的火... Purification of soul...Just a false...^_^
6 修改: 发表于 2008-05-22 17:51 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
积分 3,105
发帖 1,276
注册 2008-03-08 13:00
18年会员
UID 112398
性别 男
状态 离线

@echo off
title PORT T0 PROCESS
:1
cls&echo.&set "tmp="
set "port="
set/p "port=Input the port id: "
if not defined port goto 1
echo %port%|findstr /v /i "">nul&&call :lp||goto 1
:lp
for /f "skip=4 tokens=3,7 delims=: " %%a in ('netstat -anop tcp') do (
echo %%a|findstr /c:"%port%">nul&&(set tmp=A&echo.&echo PROCESS INFOMATION:&echo.&tasklist /fi "pid eq %%b" 2>nul)
)
if not defined tmp echo.&echo No %port% port running
pause>nul&goto 1
批处理之家新域名:www.bathome.net
7 发表于 2008-05-22 17:54 ·  中国 山东 淄博 联通
银牌会员
★★★
积分 1,604
发帖 646
注册 2008-04-13 23:39
18年会员
UID 115804
性别 男
状态 离线
Originally posted by zw19750516 at 2008-5-22 05:38 PM:
学批处理的新手怕是难以理解啊,兄弟。
ps:好像不对啊,我的机器上196端口是开着的,但运行后却提示no run!

[ Last edited by zw19750516 on 2008-5-22 at 05:46 PM ]


啊! 抱歉抱歉,忘记说了~ 这段代码只能检测TCP端口~ - -||||
心绪平和,眼藏静谧,无比安稳的火... Purification of soul...Just a false...^_^
8 发表于 2008-05-22 17:57 ·  中国 山东 淄博 联通
银牌会员
★★★
积分 1,604
发帖 646
注册 2008-04-13 23:39
18年会员
UID 115804
性别 男
状态 离线
findstr /c 不是匹配多个字符串吗?~
findstr "\<%port%\>" 不是精确匹配吗~



PS。前辈的代码貌似也有点问题,我没开35端口,只开着135 但输入35 却跟输入135 输出同样的结果~ SVCHOST.EXE

[ Last edited by pusofalse on 2008-5-22 at 06:05 PM ]
心绪平和,眼藏静谧,无比安稳的火... Purification of soul...Just a false...^_^
论坛跳转: