繁体   English   中英

sed 命令 UNIX Groovy

[英]sed command UNIX Groovy

我正在从 groovy 执行 schellscript update.sh。shellscript 应该使用 sed 命令更新另一个 XML 文件,但是当我使用 groovy 运行时没有发生 XML 更新,但是当我从命令行运行 shellscript 时发生了

时髦的

def proc = ['/move/update.sh',name].execute()

脚本

NAME=$1
sed -i "s/ITEM_NAME/$1/g" script.xml

当我使用 groovy 运行时,script.xml 没有更新

你必须弄清楚这个产生的过程发生了什么,而不是问我们。 看看这个例子:

$ groovysh
Groovy Shell (3.0.4, JVM: 14.0.1)
Type ':help' or ':h' for help.
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
groovy:000> proc = ['sh', '-c', 'echo "this is stdout"; echo "this is stderr">&2; exit 3'].execute()
===> Process[pid=29552, exitValue="not exited"]
groovy:000> proc.err
===> java.lang.ProcessImpl$ProcessPipeInputStream@e154848
groovy:000> proc.err.getText()
===> this is stderr

groovy:000> proc.getText()
===> this is stdout

groovy:000> proc.exitValue()
===> 3

该 shell 脚本是不必要的:groovy 可以直接调用:鉴于此

$ cat script.xml
<item name="ITEM_NAME"/>
$ cat sed.groovy
def name = 'this is the item name'
def cmd = ['sed', '-i', '', "s/ITEM_NAME/${name}/", 'script.xml']
println cmd
def proc = cmd.execute()
println proc.exitValue()
println proc.err.getText()
println proc.getText()

请注意,我在 Mac 上使用 sed 的-i选项需要一个值。 如果您使用 GNU sed,请调整该代码。

运行它看起来像这样

$ groovy sed.groovy
[sed, -i, , s/ITEM_NAME/this is the item name/, script.xml]
0


$ cat script.xml
<item name="this is the item name"/>

另一方面,groovy 可以读取文件、进行文本替换和写入文件,因此您根本不需要调用 sed。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM