简体   繁体   中英

sed command UNIX Groovy

I am executing a schellscript update.sh from groovy.the shellscript should update another XML file using sed command,but the XML update is not happening when I run using groovy ,but it is happening when I run the shellscript from command line

groovy

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

shellscript

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

script.xml is not getting updated when I run using groovy

You have to figure out what is happening with that spawned process instead of asking us. Have a look at this example:

$ 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

That shell script is unnecessary: groovy can invoke directly: Given this

$ 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()

Note that I'm on a Mac where sed's -i option requires a value. Adjust that code if you use GNU sed.

Running it looks like this

$ 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"/>

On the other hand, groovy can read a file, make text replacements, and write a file, so you don't need to call out to sed at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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