运行批处理脚本来终止进程的问题
我在命令行上使用以下脚本来终止假设的记事本进程(如果重要的话,在 Windows XP 中使用 KornShell (ksh)):
kill $(tasklist | grep -i notepad.exe | awk '{print 2}')
现在我将这一行放入批处理文件 c:\temp\testkill.bat 中,认为我也应该能够通过运行批处理文件来终止进程。但是,当我运行批处理文件时,出现以下关于不平衡括号的 awk 错误:
C:/温度> ./testkill.bat
C:\Temp>kill $(tasklist | grep -i notepad.exe | awk '{print $2}')
awk: unbalanced () 上下文是:
>>>>> {打印 $2}) <<<
C:/温度>
因此,我很困惑为什么当我通过批处理文件运行此脚本时会收到有关不平衡括号的错误,但当我直接从命令行运行命令时却没有问题?
(我不一定与这种终止进程的方式联系在一起 - 我还想知道为什么如果我在命令行上编写以下内容:
任务列表| grep -i 记事本.exe | awk '{print $2}' |杀死
从tasklist/grep/awk 调用中产生的进程ID 似乎没有正确通过管道进行终止
I am using the following script on a command line to kill a hypothetical notepad process (using a KornShell (ksh) in Windows XP, if that matters):
kill $(tasklist | grep -i notepad.exe | awk '{print 2}')
Now I take this line, and put it into a batch file c:\temp\testkill.bat, thinking that I should just as well be able to kill the process by running the batch file. However, when I run the batch file, I get the following awk error about unbalanced parentheses:
C:/Temp> ./testkill.bat
C:\Temp>kill $(tasklist | grep -i notepad.exe | awk '{print $2}')
awk: unbalanced () Context is:
>>> {print $2}) <<<
C:/Temp>
So I am baffled as to why I am getting this error about unbalanced parentheses when I run this script via a batch file, but have no issues when I run the command directly from the command line?
(I am not necessarily tied to this way of killing a process - I am additionally wondering why if I write the following on the command line:
tasklist | grep -i notepad.exe | awk '{print $2}' | kill
The process ID that comes out of the tasklist/grep/awk calls does not seem to properly get piped to kill.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您有 Korn shell,为什么还要创建批处理文件?编写一个 shell 脚本 - 这可能会对您有很大帮助。
我可以回答你的最后一个问题 -
kill
不会从标准输入中获取要杀死的 PID,而是在命令行上获取它。您可以使用 xargs 来使其工作:Why are you making a batch file if you have a Korn shell? Write a shell script - that will probably help you out a lot.
I can answer your final question -
kill
doesn't take the PID to kill from the standard input, it takes it on the command line. You can usexargs
to make it work: