rubyshellpopen3

How to call function from shell script from ruby


How to call function in shell script from ruby (preferably using open3)

#!/bin/sh
# A simple script with a function...

function add()
{
  echo "1"
}

Ruby Script that does not work--

#!/apollo/bin/env ruby
# -*- ruby -*-   
require 'open3'
Open3.capture3('.\something.sh', 'add')

Solution

  • In the first place, you should have a valid bash function declaration.

    Assuming something.sh was corrected to:

    #!/bin/sh
    # A simple script with a function...
    
    bar () {
      echo "1"
    }
    

    You have to load it’s content into current shell and execute a function in it:

    Open3.capture3(". ./something.sh && bar")
    #⇒ ["1\n", "", #<Process::Status: pid 17113 exit 0>]