Groovy 进程不适用于 linux shell(grep、awk 和 ps) [英] Groovy process not working with linux shell (grep and awk and ps)

查看:39
本文介绍了Groovy 进程不适用于 linux shell(grep、awk 和 ps)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Process proc1 ='sh -c ps -ef'.execute();
Process proc2 ='sh -c grep sleep.sh '.execute();
Process proc3 ='sh -c grep -v grep '.execute();
Process proc4 ='sh -c awk sleep.sh '.execute();

Process all = proc1 | proc2 | proc3 | proc4;

// I tried this too and this didnt work

//println( [ 'sh', '-c', 'ps -ef | grep "sleep.sh" | grep -v "grep" |     awk "sleep.groovy" ' ].execute().text )

//also tried without the awk

println all.text;

好的,所以我想要做的是 ps 我制作的 shell 脚本 (sleep.sh) [它会休眠一段时间].不太确定如何做到这一点.这是我最好的猜测^^

Okay so what I am trying to do is ps the shell script i made (sleep.sh) [all it does it sleep for a period of time]. Not quite sure how to do that. This was my best guess^^

结果:

-sh-3.2$ ./callGroovy.sh testSleep.groovy


-sh-3.2$

不打印任何东西,也不给我任何东西(callGroovy 是我用来调用我的 groovy 脚本的 shell 脚本)如果我运行管道命令,它们仍然可以工作,除了 awk我想我做错了 awk继承人其余的管道

doesnt print anything out and doesnt give me anything (callGroovy is a shell script i use to call my groovy script) If i run the piped commands they work still except the awk I think i am doing the awk wrong heres the rest piped

-sh-3.2$ ps -ef | grep "sleep.sh" | grep -v "grep"
wasadmin ***** *****  0 **:** pts/1    **:**:** /bin/bash ./sleep.sh  

(所有 * 都是数字)

(where all the * are numbers)

当我只使用 grep 和 ps 尝试脚本时,它也不会给我这个输出.有什么建议?..PS 我也试过在 groovy 脚本中使用和不使用引号.没想到它会有所作为,但值得一试

when i try the script with just the grep and ps it doesnt give me this output either. any suggestions? ..PS Also I tried with and without the quotes in the groovy script. Didnt think it would make a difference but worth a shot

推荐答案

shell -c 选项只需要一个参数.从命令行试试这个,你会看到它也失败了:

The shell -c option expects one parameter only. Try this from the command line, and you'll see it fails as well:

sh -c ps -ef | sh -c grep sleep.sh | sh -c grep -v grep | sh -c awk sleep.sh

它需要引号才能正常工作:

It needs quotes to work properly:

sh -c "ps -ef" | sh -c "grep sleep.sh" | sh -c "grep -v grep" | sh -c "awk sleep.sh"

您可以通过以字符串列表而不是字符串开头来正确引用命令:proc1 = ['sh', '-c', 'ps -ef'].在这种情况下,您在 groovy 中进行过滤,因此简单的解决方案是不通过 shell 调用命令.试试这个:

You can quote the commands properly by starting with a list of strings instead of a string: proc1 = ['sh', '-c', 'ps -ef']. In this case you're doing the filtering in groovy, so the simple solution is to simply not invoke the commands through the shell. Try this:

Process proc1 ='ps -ef'.execute()
Process proc2 ='grep sleep.sh '.execute()
Process proc3 ='grep -v grep '.execute()
Process proc4 ='awk sleep.sh '.execute()

Process all = proc1 | proc2 | proc3 | proc4

println all.text

最后,如果事情不能正常工作,使用

Finally, if things don't work properly, it can be helpful to read the stderr stream with

println all.err.text

这篇关于Groovy 进程不适用于 linux shell(grep、awk 和 ps)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆