groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Nelson, Erick" <Erick.Nel...@hdsupply.com>
Subject Re: Running a shell script with return value?
Date Wed, 30 May 2018 18:13:31 GMT


String cmd = /$

What you want to shell out and execute here

Remember, java shells out as sh, not bash or your shell of choice

/$

// output and error can be any class that implements Appendable

StringBuilder output = new StringBuilder()

StringBuilder error = new StringBuilder ()

Process proc = cmd.execute()

proc.waitForProcessOutput(output, error)

println proc.exitValue()


Erick Nelson
Senior Developer – IT
HD Supply Facilities Maintenance
(858) 740-6523


From: Chris Fouts <chrisfouts@ziftsolutions.com>
Reply-To: "users@groovy.apache.org" <users@groovy.apache.org>
Date: Wednesday, May 30, 2018 at 10:58 AM
To: "users@groovy.apache.org" <users@groovy.apache.org>
Subject: Running a shell script with return value?

Inside groovy, I want to write a shell script that parses the output of some shell command
and get some string value back. How can I get the value of the shell command?

For example say I want to get the file size of some_file, I'll do

x = sh returnStatus: true, script: '''
  IFS=" " read -ra LINE <<< `ls -al | grep some_file`
  echo ${LINE[4]}
'''

How can I store the value of ${LINE[4]} in a groovy variable to use later?

Thanks,
Chris
Mime
View raw message