Running a shell script with return value?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Running a shell script with return value?

Chris Fouts
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
Reply | Threaded
Open this post in threaded view
|

Re: Running a shell script with return value?

ericksn

       

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 <[hidden email]>
Reply-To: "[hidden email]" <[hidden email]>
Date: Wednesday, May 30, 2018 at 10:58 AM
To: "[hidden email]" <[hidden email]>
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

Reply | Threaded
Open this post in threaded view
|

Re: Running a shell script with return value?

Chris Fouts
Thanks. Do the /$ designate a begin/end fo the commands I want to execute? So using my example, I do 

String cmd = /$

  IFS=" " read -ra LINE <<< `ls -al | grep some_file`

  echo ${LINE[4]}

/$


On Wed, May 30, 2018 at 2:20 PM Nelson, Erick <[hidden email]> wrote:

       

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

<a href="tel:(858)%20740-6523" value="+18587406523" target="_blank">(858) 740-6523

 

 

From: Chris Fouts <[hidden email]>
Reply-To: "[hidden email]" <[hidden email]>
Date: Wednesday, May 30, 2018 at 10:58 AM
To: "[hidden email]" <[hidden email]>
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

Reply | Threaded
Open this post in threaded view
|

Re: Running a shell script with return value?

ericksn

I mistyped

It is …

 

$/

/$

 

Similar to

'''

'''

 

They are known as “dollar slashy strings”

 

Erick Nelson

Senior Developer – IT

HD Supply Facilities Maintenance

(858) 740-6523

 

 

From: Chris Fouts <[hidden email]>
Reply-To: "[hidden email]" <[hidden email]>
Date: Wednesday, May 30, 2018 at 11:55 AM
To: "[hidden email]" <[hidden email]>
Subject: Re: Running a shell script with return value?

 

Thanks. Do the /$ designate a begin/end fo the commands I want to execute? So using my example, I do 

 

String cmd = /$

  IFS=" " read -ra LINE <<< `ls -al | grep some_file`

  echo ${LINE[4]}

/$

 

On Wed, May 30, 2018 at 2:20 PM Nelson, Erick <[hidden email]> wrote:

       

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

<a href="tel:(858)%20740-6523" target="_blank">(858) 740-6523

 

 

From: Chris Fouts <[hidden email]>
Reply-To: "[hidden email]" <[hidden email]>
Date: Wednesday, May 30, 2018 at 10:58 AM
To: "[hidden email]" <[hidden email]>
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

Reply | Threaded
Open this post in threaded view
|

Re: Running a shell script with return value?

Chris Fouts
Thanks. Here's the complete code snippet

#!/usr/bin/env groovy

String cmd = $/

  IFS=" " read -ra LINE <<< `ls -al | grep Music`

  echo ${LINE[4]}

/$

StringBuilder outstr = new StringBuilder()

StringBuilder errstr = new StringBuilder()

Process proc = cmd.execute()

proc.waitForProcessOutput(outstr, errstr)

println $output


When I run it, I get 

Caught: groovy.lang.MissingPropertyException: No such property: LINE for class: file

groovy.lang.MissingPropertyException: No such property: LINE for class: file

at file.run(file.groovy:5)



On Wed, May 30, 2018 at 2:59 PM Nelson, Erick <[hidden email]> wrote:

I mistyped

It is …

 

$/

/$

 

Similar to

'''

'''

 

They are known as “dollar slashy strings”

 

Erick Nelson

Senior Developer – IT

HD Supply Facilities Maintenance

<a href="tel:(858)%20740-6523" value="+18587406523" target="_blank">(858) 740-6523

 

 

From: Chris Fouts <[hidden email]>
Reply-To: "[hidden email]" <[hidden email]>
Date: Wednesday, May 30, 2018 at 11:55 AM
To: "[hidden email]" <[hidden email]>
Subject: Re: Running a shell script with return value?

 

Thanks. Do the /$ designate a begin/end fo the commands I want to execute? So using my example, I do 

 

String cmd = /$

  IFS=" " read -ra LINE <<< `ls -al | grep some_file`

  echo ${LINE[4]}

/$

 

On Wed, May 30, 2018 at 2:20 PM Nelson, Erick <[hidden email]> wrote:

       

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

<a href="tel:(858)%20740-6523" target="_blank">(858) 740-6523

 

 

From: Chris Fouts <[hidden email]>
Reply-To: "[hidden email]" <[hidden email]>
Date: Wednesday, May 30, 2018 at 10:58 AM
To: "[hidden email]" <[hidden email]>
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

Reply | Threaded
Open this post in threaded view
|

Re: Running a shell script with return value?

ericksn

You might want to not use dollar slashy and use just slashy or ‘’’ as dollar slashy interpolates….or …. Just escape the dollar sign in your shell script with a \

 

Erick Nelson

Senior Developer – IT

HD Supply Facilities Maintenance

(858) 740-6523

 

 

From: Chris Fouts <[hidden email]>
Reply-To: "[hidden email]" <[hidden email]>
Date: Wednesday, May 30, 2018 at 12:38 PM
To: "[hidden email]" <[hidden email]>
Subject: Re: Running a shell script with return value?

 

Thanks. Here's the complete code snippet

 

#!/usr/bin/env groovy

String cmd = $/

  IFS=" " read -ra LINE <<< `ls -al | grep Music`

  echo ${LINE[4]}

/$

StringBuilder outstr = new StringBuilder()

StringBuilder errstr = new StringBuilder()

Process proc = cmd.execute()

proc.waitForProcessOutput(outstr, errstr)

println $output

 

When I run it, I get 

Caught: groovy.lang.MissingPropertyException: No such property: LINE for class: file

groovy.lang.MissingPropertyException: No such property: LINE for class: file

at file.run(file.groovy:5)

 

 

On Wed, May 30, 2018 at 2:59 PM Nelson, Erick <[hidden email]> wrote:

I mistyped

It is …

 

$/

/$

 

Similar to

'''

'''

 

They are known as “dollar slashy strings”

 

Erick Nelson

Senior Developer – IT

HD Supply Facilities Maintenance

<a href="tel:(858)%20740-6523" target="_blank">(858) 740-6523

 

 

From: Chris Fouts <[hidden email]>
Reply-To: "[hidden email]" <[hidden email]>
Date: Wednesday, May 30, 2018 at 11:55 AM
To: "[hidden email]" <[hidden email]>
Subject: Re: Running a shell script with return value?

 

Thanks. Do the /$ designate a begin/end fo the commands I want to execute? So using my example, I do 

 

String cmd = /$

  IFS=" " read -ra LINE <<< `ls -al | grep some_file`

  echo ${LINE[4]}

/$

 

On Wed, May 30, 2018 at 2:20 PM Nelson, Erick <[hidden email]> wrote:

       

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

<a href="tel:(858)%20740-6523" target="_blank">(858) 740-6523

 

 

From: Chris Fouts <[hidden email]>
Reply-To: "[hidden email]" <[hidden email]>
Date: Wednesday, May 30, 2018 at 10:58 AM
To: "[hidden email]" <[hidden email]>
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

MG
Reply | Threaded
Open this post in threaded view
|

Re: Running a shell script with return value?

MG
In reply to this post by Chris Fouts
http://groovy-lang.org/syntax.html#all-strings


On 30.05.2018 21:38, Chris Fouts wrote:
Thanks. Here's the complete code snippet

#!/usr/bin/env groovy

String cmd = $/

  IFS=" " read -ra LINE <<< `ls -al | grep Music`

  echo ${LINE[4]}

/$

StringBuilder outstr = new StringBuilder()

StringBuilder errstr = new StringBuilder()

Process proc = cmd.execute()

proc.waitForProcessOutput(outstr, errstr)

println $output


When I run it, I get 

Caught: groovy.lang.MissingPropertyException: No such property: LINE for class: file

groovy.lang.MissingPropertyException: No such property: LINE for class: file

at file.run(file.groovy:5)



On Wed, May 30, 2018 at 2:59 PM Nelson, Erick <[hidden email]> wrote:

I mistyped

It is …

 

$/

/$

 

Similar to

'''

'''

 

They are known as “dollar slashy strings”

 

Erick Nelson

Senior Developer – IT

HD Supply Facilities Maintenance

<a href="tel:%28858%29%20740-6523" value="+18587406523" target="_blank" moz-do-not-send="true">(858) 740-6523

 

 

From: Chris Fouts <[hidden email]>
Reply-To: "[hidden email]" <[hidden email]>
Date: Wednesday, May 30, 2018 at 11:55 AM
To: "[hidden email]" <[hidden email]>
Subject: Re: Running a shell script with return value?

 

Thanks. Do the /$ designate a begin/end fo the commands I want to execute? So using my example, I do 

 

String cmd = /$

  IFS=" " read -ra LINE <<< `ls -al | grep some_file`

  echo ${LINE[4]}

/$

 

On Wed, May 30, 2018 at 2:20 PM Nelson, Erick <[hidden email]> wrote:

       

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

<a href="tel:%28858%29%20740-6523" target="_blank" moz-do-not-send="true">(858) 740-6523

 

 

From: Chris Fouts <[hidden email]>
Reply-To: "[hidden email]" <[hidden email]>
Date: Wednesday, May 30, 2018 at 10:58 AM
To: "[hidden email]" <[hidden email]>
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